I have a bit of a pickle. I'm not sure how standard my setup is, so I'll give it a quick overview.
We had a project in which we've set up both MVC and API serving endpoint. It works great. I've read recently that you can mix Blazor components into the MVC project using something like this:
@(await Html.RenderComponentAsync<BlazorComponents.Components.SelectExmpleComponent>(RenderMode.WebAssemblyPrerendered, new {Options = operatorRoles}))
This actually works great! I had to set up a Blazor client project and mess around with the Startup configurations, but I was successful at getting Blazor to load pre-rendered almost instantly. It then downloads required components in the background and all bindings work. I'm very happy with this and I can see this becoming a huge boost in productivity.
Just as I was ready to call it a roaring success, we found a pickle. Apparently, all my initial tests were done on the default controller. As soon as I move it to a different controller, the browser appends the controller path to all the outgoing requests. I can get around it for the initial JavaScript import:
<script src="@Url.Content("~/_framework/blazor.webassembly.js")"></script>
But this script internally loads other components, the first one being _framework/blazor.boot.json for example and I get:
Failed to load resource: the server responded with a status of 404 () _framework/blazor.boot.json
On further inspection, it turns out that the browsers is asking for [Root Domain]/[Controller]/_framework/blazor.boot.json , which obviously is incorrect.
I'm unsure how to fix this. One workaround that we currently employ is to not use the controller in the routes and ensure that each route is unique... But that's a little hacky to my taste, I liked the separation into controllers. I could also put all the dependant files manually and put them into a static resources folder and reference them through a @Url.Content() directive, but that also feels a little hacky.
Is there a cleaner way? For reference, I'll include my current Configure method from the Startup:
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
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.UseSession();
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseBlazorFrameworkFiles();
app.UseRouting();
app.UseAuthorization();
app.UseAuthentication();
app.UseEndpoints(endpoints =>
{
endpoints.MapRazorPages();
endpoints.MapControllers();
endpoints.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}");
});
}