简体   繁体   中英

Blazor routing - navigating to a static page

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?

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.

This behavior is due to the fact that the Router, using the UriHelper search for components within the document base uri ( <base href="/" /> ). A temporary solution may be possible by providing absolute uri to your links that are not components, as for instance: <a href="https://www.asp.net/">Asp Net<a>

Try This; Note: The code should be placed in the Configure method of your server-side app

// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {
--- ---- ---
            // Mount the server-side Blazor app on /subdir
            app.Map("/subdir", subdirApp =>
            {

                subdirApp.UseBlazor<YourBlazorApp.Startup>();
            });
        } 

Source: https://github.com/aspnet/AspNetCore/blob/master/src/Components/test/testapps/TestServer/Startup.cs

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM