asp.net 自定义错误应该如何工作?
我按照 这篇文章在我的 asp.net mvc 应用程序中实现了自定义错误。我注意到,如果我访问 http://www. mysite.com/some-non-existent-controller-and-action 我按预期收到了 404 错误页面。然而,看看 Firebug 发生的情况,我发现我收到了针对不存在页面的 302 Found 响应,然后该响应重定向到我的自定义错误页面,然后返回 404(并显示自定义错误页面)。这是对的吗?我不认为首先返回的 302 非常好,特别是从 SEO 角度来看,也许我需要重新考虑一下我是如何实现这一点的。
I have implemented custom errors in my asp.net mvc application by following this article. What I have noticed is that if I go to http://www.mysite.com/some-non-existent-controller-and-action I get my 404 error page as expected. However, looking at what happens with firebug, I see that I get a 302 Found response for the non-existent page, which then redirects to my custom error page which then returns with a 404 (and displays the custom error page). Is this right? I don't think the 302 that is first returned is very good especially from an SEO perspective, and that maybe I need to think again about how I have implemented this.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
处理 404 的最佳指南(我认为)可以在 这个答案。基本上,404 发生的方式有多种:
{controller}/{action}/{parameter}
规则。HandleUnknownAction
覆盖处理。链接的答案基本上设置了一个控制器,可以从代码中的任何点执行,而无需重写 URL - 这就是您想要的。
此外,您还应该考虑处理未处理的异常和错误的 URL(例如包含尖括号等不安全字符的 URL)。在这种特殊情况下,您必须重写 URL,否则您根本无法呈现响应。这些特定的请求有点棘手,我在博客中提到了此处。
The best guide(i think) for handling 404s can be found in this answer. Basically there are multiple ways in which 404s can happen:
{controller}/{action}/{parameter}
rule.HandleUnknownAction
override.The linked answer basically sets up a controller that can be executed from any point in the code without rewriting the URL - which is what you want.
In addition, you should also think about handling unhandled exceptions and bad URLs (like the ones containing unsafe characters like angle brackets). I that particular case you have to rewrite the URL, otherwise you can't render the response at all. These particular requests are kind of tricky, i blogged about that here.
您是否按照页面底部的建议添加了映射到您的“NotFound”操作的“包罗万象”的路由:
如果您将此作为您添加的最后一个路由,则任何“未知”URL 将直接映射到您的对
ErrorController
执行“NotFound”操作,您可以直接从那里返回“未找到”视图,无需重定向。Did you follow the advice down towards the bottom of the page adding a "catch-all" route that maps to your "NotFound" action:
If you make this the very last route you add, any "unknown" URLs will map directly to your "NotFound" action on the
ErrorController
and you can just return the "not found" view directly from there, no redirects required.