Blazor:实施404找不到页面 [英] Blazor: Implementing 404 not found page

查看:406
本文介绍了Blazor:实施404找不到页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的应用程序内部实现一个页面,当blazor路由器找不到匹配的路由时,该页面就会出现。目前,所有请求都路由到 index.html ,因此我无法像平常一样通过iis处理错误。如果输入无效的路由,将显示空白页(实际上是 index.html )并收到控制台错误:



路由器找不到带有 / some / Nonexistent / Route路由的任何组件。



由于blazor路由器能够识别出没有定义与请求的路由相匹配的路由,因此我似乎应该能够处理此问题,但是到目前为止,我还没有找到任何关于此的文档。

如何实现?有没有一种方法可以连接到blazor路由器,并将所有未找到的路由定向到预定义的错误路由?



我看到了 https://github.com/aspnet/AspNetCore/issues/5489 列出了404处理程序的问题,但我不确定如果那是为了使某些东西比我想要做的更坚固,生产就绪

解决方案

请尝试以下操作:
App.cshtml

 < Router AppAssembly = typeof(Program)。AssemblyFallbackComponent = typeof(Error404)> 

创建一个名为Error404.cshtml的组件



注意:这只是我从挖掘Router类收集的猜测。参见 https:// github.com/aspnet/AspNetCore/blob/343208331d9ebbb3a67880133f4139bee2cb1c71/src/Components/src/Microsoft.AspNetCore.Components/Routing/Router.cs



请,请让我知道这对你有没有用。


I'd like to implement a page internal to my application that appears when the blazor router is unable to find a matching route. Currently, all requests are routing to index.html so I'm unable to handle errors through iis as I may normally. If I enter an invalid route, I will be shown a blank page (which is actually index.html) and receive a console error:

'Router' cannot find any component with a route for '/some/Nonexistent/Route'.

It seems like I should be able to handle this since the blazor router is able to recognize that there is no route defined which matches the requested route however I've so far been unable to find any documentation on this.

How can I implement this? Is there a way to hook into the blazor router and direct all non found routes to a predefined error route?

I see https://github.com/aspnet/AspNetCore/issues/5489 has an issue listed for a 404 handler however I'm not sure if that is for something more robust and production ready than what I'm looking to do

解决方案

Try this: App.cshtml

<Router AppAssembly=typeof(Program).Assembly FallbackComponent="typeof(Error404)" >

Create a Component named Error404.cshtml

Note: This is only a guess I gathered from digging the Router class. See https://github.com/aspnet/AspNetCore/blob/343208331d9ebbb3a67880133f4139bee2cb1c71/src/Components/src/Microsoft.AspNetCore.Components/Routing/Router.cs

Please, let me know if it works for you.

这篇关于Blazor:实施404找不到页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆