You're almost there. I'm not sure what you're doing with the root site so I've added a simple landing page with a link to the WASM SPA. Here's a full set on instructions.
- host.html - change base to
<base href="/app/" />
. This will make sure all the @Page
directives in the SPA get prefixed with app. You need the trailing /
.
- host.html - change the script reference to
<script src="/app/_framework/blazor.webassembly.js"></script>
. You'll get away with not changing it if you're hosting a single WASM SPA. Experiment.
- WASM Project file Add
StaticWebAssetBasePath
. This sets up the build with the correct path.
<PropertyGroup>
<StaticWebAssetBasePath>app</StaticWebAssetBasePath>
<TargetFramework>net5.0</TargetFramework>
</PropertyGroup>
- Update
Startup
in the Server project, adding a new middleware path for mywebsite/app
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
app.UseWebAssemblyDebugging();
}
else
{
app.UseExceptionHandler("/Error");
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}
app.UseHttpsRedirection();
app.MapWhen(ctx => ctx.Request.Path.StartsWithSegments("/app"), first =>
{
first.UseBlazorFrameworkFiles("/app");
first.UseStaticFiles();
first.UseRouting();
first.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
endpoints.MapFallbackToFile("app/{*path:nonfile}", "app/index.html");
});
});
app.UseRouting();
app.UseEndpoints(endpoints =>
{
endpoints.MapRazorPages();
endpoints.MapControllers();
// endpoints.MapFallbackToFile("_index.cshtml");
endpoints.MapFallbackToPage("/_Index");
});
}
I've added a default landing page to the root site - _Index.cshtml
@page "/"
@model WebApplication2.Server.Pages._IndexModel
<h3>Hello App</h3>
<div><a href="/app">App WASM SPA</a></div>
@{
}
Note FetchData won't work until you update the controller url
forecasts = await Http.GetFromJsonAsync<WeatherForecast[]>("/WeatherForecast");
You can also move the WASM wwwroot into the root and add the startup page at say _App.cshtml to launch the SPA.
For more information there's a Github site here by Javier Nelson and an article I wrote on how to host multiple SPAs on the same website here with a Github Repo.
dotnet run
command. But when dockerized, I see the requests twice: one with the correct path (localhost/app/{ressourse}
) which results in a 200OK response, and one with the pathlocalhost/app/app/{ressource}
which results in a 404 response. Any chance you know why I'm having that? – Micahmicawber