I've looked at the answer from Daz, which does fix the problem when debugging/running a standalone product. My production environment is Kubernetes based however, and my ingress controller wouldn't route the request to the correct location e.g:
In my example, I have changed the usual 4 settings:
- set the <base href> in the index file to <base href="/app/" />
- set app.UseBlazorFrameworkFiles(new PathString("/app")) in the servers ConfigureServices function
- set endpoints.MapFallbackToFile("/app/{*path:nonfile}", "app/index.html") in the servers Configure function, UseEndpoints option
- added <StaticWebAssetBasePath>app into the client's <PropertyGroup> section of the csproj file.
Everything was working barring the single file "Microsoft.AspNetCore.Components.WebAssembly.Authentication/AuthenticationService.js" which is an embedded file.
Since the file needs to come from the server, adjusting the file path in the client won't work, because it wouldn't be correctly routed by my ingress configuration. For my application, I dropped in a very simple middleware as follows:
// "/{app}/_content/{embedded-library-files}/{file}" requests need to be presented at root
app.Use(async (context, next) =>
{
HttpRequest request = context.Request;
var path = request.Path;
if (path.StartsWithSegments(new PathString("/app/_content")))
{
if (new string[] {
"/Microsoft.AspNetCore.Components.WebAssembly.Authentication/AuthenticationService.js"
}.Any(s => path.Value.EndsWith(s)))
{
request.Path = path.Value.Substring("/app".Length);
}
}
await next.Invoke();
});
You'd need an if clause for each folder, and an entry in the list for each file. Obviously this can be simplified if you know that the whole folder should be redirected.
Since rewriting the path in the server is invisible to the client application, this simple update fixes the problem.
Mark