简体   繁体   中英

Razor page routing and FileSystem routing

I have a razor page MyPage.cshtml with the following annotation

@page "{id}"

It is there so I can have a route that looks like this;

/MyPage/Blah

This all works. When I see stuff like the following;

/MyPage/Blah/content/somefile.js

it results in a 404 .
I would like that to be handled by the filesystem. I do have the following file in;

wwwroot/MyPage/Blah/content/somefile.js

I am using asp.net core 3.1

Firstly,I test the code,and I can work.

And then I find if I delete app.UseStaticFiles(); in my startup.cs.I can get the same error with you. So you can check if you can access other file in wwwwroot.If not,you can check your startup.cs,and add app.UseStaticFiles(); like this in Configure:

            app.UseHttpsRedirection();
            app.UseStaticFiles();

            app.UseRouting();

Don't have other routes that mess with static file routes. ie UseBlazorFrameworkFiles was my issue.

Ok, so out of the box asp.net core 3.1 will route to my page;

/MyPage

and if I have a bunch of static files in

wwwroot/MyPage/some.js

it will pull that too. All good.

I was getting other route problems when my page was hosting a blazor app and had non-file client side routes.

To make sure routes where being properly handled in all those cases I had to add the following to my razor page.

@page "{id}/{*path:nonfile}"

So when I got a whole page refresh for

/MyPage/MyBlazorApp/counter

My page now handles the /MyPage/MyBlazorApp portion and the rest gets handed off to the client-side blazor app to finally go to the client-side route: /MyPage/MyBlazorApp/counter

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