Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix the BlazorWasmAspNetCoreHosted article #288

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
102 changes: 100 additions & 2 deletions BlazorWasmAspNetCoreHosted/README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,103 @@
# Blazor WebAssembly Asp.NET Core Hosted

This is a sample project that demonstrates how to create a Blazor WebAssembly application with an Asp.NET Core Hosted project. See the article that explains this project:
Microsoft provides a template named Blazor WebAssembly Asp.NET Core Hosted. This template is an Asp.NET Core Razor Pages application that hosts a Blazor WebAssembly application. Basically, `HttpApi.Host` and `Blazor` applications are hosted together. In this case, only one application will be deployed and blazor application will be served by the HttpApi.Host.

**https://abp.io/community/articles/blazor-webassembly-asp.net-core-hosted-zbjvgrc9**
## Instructions

- Create a new ABP Application with Blazor UI

```bash
abp new BookStore -u blazor -t app -v 6.0.0-rc.2 --no-random-port
```

- Add Blazor project reference and `Microsoft.AspNetCore.Components.WebAssembly.Server` package reference to the HttpApi.Host project.
```xml
<ItemGroup>
<ProjectReference Include="..\BookStore.Blazor\BookStore.Blazor.csproj" />
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.Server" Version="6.0.8" />
</ItemGroup>
```

- Add Blazor framework files middleware into **OnApplicationInitializaiton** method in `BookStoreHttpApiHostModule.cs` file.
```csharp
public override void OnApplicationInitialization(ApplicationInitializationContext context)
{
// ...

app.UseBlazorFrameworkFiles(); // 👈 Make sure it's before UseStaticFiles()

app.UseStaticFiles();

// ...
}
```

- Add a mapping for fallback to index.html file at the end of the **OnApplicationInitializaiton** method in `BookStoreHttpApiHostModule.cs` file.

```csharp
if (app is WebApplication webApp)
{
webApp.MapFallbackToFile("index.html");
}
```

- Configure your blazor SelfUrl as HttpApi.Host URL in `BookStore.Blazor/wwwroot/appsettings.json`

```json
{
"App": {
"SelfUrl": "https://localhost:44305"
},
"AuthServer": {
"Authority": "https://localhost:44305",
"ClientId": "BookStore_Blazor",
"ResponseType": "code"
},
"RemoteServices": {
"Default": {
"BaseUrl": "https://localhost:44305"
}
},
"AbpCli": {
"Bundle": {
"Mode": "BundleAndMinify", /* Options: None, Bundle, BundleAndMinify */
"Name": "global",
"Parameters": {

}
}
}
}
```

- Configure DbMigrator too. Navigate to `BookStore.DbMigrator/appsettings.json` and change Blazor URL to HttpApi.Host URL.

```json
{
"ConnectionStrings": {
"Default": "XXX"
},
"OpenIddict": {
"Applications": {
"BookStore_Blazor": {
"ClientId": "BookStore_Blazor",
"RootUrl": "https://localhost:44305"
},
"BookStore_Swagger": {
"ClientId": "BookStore_Swagger",
"RootUrl": "https://localhost:44305"
}
}
}
}
```

- Run `BookStore.DbMigrator` once.

- Remove **HomeController.cs** from HttpApi.Host project to prevent `/swagger` redirection.

- Run only `BookStore.HttpApi.Host` project and see the result.

![blazor-aspnetcore-hosted-demo](blazor-aspnetcore-hosted-demo.gif)

As you can see, URL is `localhost:44305` for blazor application and login razor page. MVC application and Blazor WebAssembly works together. As you can see swagger UI is available at `localhost:44305/swagger` too.