由于某种原因,App.razor
当用户在浏览器上键入文件之类的端点(例如:localhost:5001/unexist.file)时,Blazor 不显示在 中创建的“未找到”页面
App.razor
<CascadingAuthenticationState>
<Router AppAssembly="@typeof(Program).Assembly" >
....
<NotFound>
<LayoutView Layout="@typeof(MainLayout)">
...
My custom Not Found page content
...
</LayoutView>
</NotFound>
</Router>
<NavigationTracker />
</CascadingAuthenticationState>
仅当端点不包含任何点字符时才能正常工作。有可能控制这个吗?
查看 Microsoft doc 我发现了这个有用的信息
基本上该方法取决于托管模型:
在您的情况下, Blazor 服务器(如您的标签所述)。为此,我们需要更改 program.cs 如下:
app.MapFallbackToPage("/{param?}", "/_Host");
在ASP.Net 中托管的 Blazor Web 程序集中,我们将其更改如下:
app.MapFallbackToFile("/{param?}", "index.html");
微软文档的解释:
服务器端默认路由模板假定如果请求 URL 的最后一段包含点 (.),则表示请求了文件。例如,URL https://localhost.com:5001/example/some.thing被路由器解释为对名为 some.thing 的文件的请求。没有额外配置,应用返回 404
我在搜索时发现的最后一件事是,在没有 dotnet 主机的情况下独立运行 Web 程序集时,我无法更改路由行为。如果我要发布它,它可以根据主机进行更改,例如更改 web.config,但是在调试模式下我没有运气。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句