Blazor routing - navigating to a static page

Viewed 2643

I am trying to create a Blazor app for use on a website. Most of the website will be static, and I would like this part to be served from static HTML files for client-side performance reasons. A few of the website’s pages will be rendered by the Blazor application. When prototyping this, I ran into a problem: when navigating away from the Blazor part back to the static part of the website, I get “Error: System.InvalidOperationException: 'Router' cannot find any component with a route for '/index.html'.”

The link to index.html is in the body tag of the page which contains the app, but outside of the app tag and it should be outside of Blazor’s attention. I understand that the cause of the problem is that the router can’t find the component for the route, but why is Blazor trying to find the component instead of just letting the browser to navigate away to a non-Blazor page? This link should be handled by the browser, not Blazor, I would have thought?

1 Answers

In the startup.cs of your site, change the app.UseBlazor() statement to:

app.Map("/bzr", child => { child.UseBlazor<Blazor.Program>(); });

And in the index.html in the wwwroot, alter the base href to:

<base href="/bzr/" />

Now anything outside of the /bzr folder will just work as a regular website.

Related