Quantcast
Channel: Active questions tagged blazor - Stack Overflow
Viewing all articles
Browse latest Browse all 4839

Host Blazor Hosted WebAssembly into a subpath

$
0
0

I am coding a Blazor Hosted WebAssembly which means that there are three projects: the client, the server and the shared project. The intercommunication happens with web apis.

The intention is to host this application into a domain.com/app instead of domain.com, approach this using subdomain is working but the requirement is to do it using subpath, so I have searched a lot and I concluded in the below changes:

Change Relative Links into pages, a hyperlink like this /user/view need to be transformed into /app/user/view

  1. this can be done manual/static by adding the prefix /app into all links in every page of the client project

  2. this can be done manual/dynamic by adding the following code into Program.cs of the server project (and call this method in all links in all pages)Code for point 2:

    public static class UrlHelperExtensions{public static string SubfolderLink(string path){var subfolder = Environment.GetEnvironmentVariable("SUBFOLDER") ?? "defaultSubfolder";return $"/{subfolder}/{path.TrimStart('/')}";}}

Change Relative Links into apis, a get or post request like this /user/view need to be transformed into /app/user/view

  1. this can be done manual/static by adding the prefix /app into all requests in every page of the client project
  2. this can be done automatic/dynamic by adding the following code into Program.cs of the client project

Code for point 2:

builder.Services.AddScoped(sp => new HttpClient(new CustomHttpClientHandler()){    BaseAddress = new Uri(builder.HostEnvironment.BaseAddress +"/app/")});

Change Relative Pages of the server like Error server page

  1. this can be done manual/static be adding a folder with name appunder the folder Pages of server project

  2. this can be done automatic/dynamic by adding the following code into Program.cs of the server projectCode for point 2:

    builder.Services.AddRazorPages(options =>{options.Conventions.AddFolderRouteModelConvention("/", model =>{foreach (var selector in model.Selectors){if (selector.AttributeRouteModel != null){ selector.AttributeRouteModel.Template = "app/" + selector.AttributeRouteModel.Template; }}});});

Content like css and js need to be served from the subfolder

this can be done by adding the following code into the index.html of the client project

<base href="/app/" />

this can be done by adding the following code into the Client.csproj file

StaticWebAssetBasePath>app</StaticWebAssetBasePath>

A challenge into this approach is to make this dunamically parameterized using a variable, this brings additional challenge into the solution as this need to be implemented using the “automatic” approach of the links and requests but brings “headacke” when it comes with the static content where index.html and Client.csproj do not support variables.

Any suggestions or alternatives?


Viewing all articles
Browse latest Browse all 4839

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>