简体   繁体   English

Razor 页面路由和文件系统路由

[英]Razor page routing and FileSystem routing

I have a razor page MyPage.cshtml with the following annotation我有一个带有以下注释的 razor 页面MyPage.cshtml

@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 .它导致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我正在使用 asp.net 核心 3.1

Firstly,I test the code,and I can work.首先,我测试了代码,我可以工作了。

And then I find if I delete app.UseStaticFiles();然后我发现我是否删除app.UseStaticFiles(); in my startup.cs.I can get the same error with you.在我的 startup.cs 中。我可以得到与你相同的错误。 So you can check if you can access other file in wwwwroot.If not,you can check your startup.cs,and add app.UseStaticFiles();所以你可以检查你是否可以访问 wwwwroot 中的其他文件。如果不能,你可以检查你的 startup.cs,并添加app.UseStaticFiles(); like this in Configure:在配置中像这样:

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

            app.UseRouting();

Don't have other routes that mess with static file routes.不要有其他与 static 文件路由混淆的路由。 ie UseBlazorFrameworkFiles was my issue.即 UseBlazorFrameworkFiles 是我的问题。

Ok, so out of the box asp.net core 3.1 will route to my page;好的,开箱即用的 asp.net 核心 3.1 将路由到我的页面;

/MyPage

and if I have a bunch of static files in如果我有一堆 static 文件

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.当我的页面托管 blazor 应用程序并且具有非文件客户端路由时,我遇到了其他路由问题。

To make sure routes where being properly handled in all those cases I had to add the following to my razor page.为了确保在所有这些情况下正确处理路由,我必须将以下内容添加到我的 razor 页面。

@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我的页面现在处理/MyPage/MyBlazorApp部分,rest 被移交给客户端 blazor 应用程序,最后 go 移交给客户端路由: /MyPage/MyBlazorApp/counter

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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