5

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
  • 1
    If you inject NavigationManager into your page and use the NavigateTo(string uri, bool forceLoad) that will do a "real" navigation, so a request will be made to the server. As long as your server returns the page you want, that should do what you want. Commented Nov 28, 2019 at 19:47

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

5 Comments

Thank you Flores. I found your other answer to a similar question and your blog post. It appears however that your solution probably applies to an earlier version of Blazor? I have the latest version (0.7) and in the Configure method there is no app.UseBlazor() line. Also, the signature of the Configure method is Configure(IBlazorApplicationBuilder app), and the IBlazorApplicationBuilder interface does not have a Map method... Am I looking at the right place?
Hi Flores, your suggestion seems to be only applicable for a Blazor app with a server side - do you know a solution for pure client-only Blazor app? Perhaps there is a way to add mappings to the router component?
I'm pretty sure I've used this with client side blazor.. but that was indeed a older version..
No, it is not an older version. And you've used it in the Configure method of the server-side Blazor (Note that I do not mean Razor Components but a Blazor client app hosted on a server, though it is also applicable to Razor Components).
Continue... But comrade Vitaly clearly says that his application is stand alone. No server is involved. I've already suggested that he uses Absolute Uri for his links to static HTML files, and thus, I believe, let the Router understand that the link is not to a Component. True or wrong, I don't think Blazor has a solution to that yet, but I believe it can easily be hacked if you inspect the Router object and WebAssemblyUriHelper as well as the JavaScript involved

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.