asp.net 自定义错误应该如何工作?

发布于 2024-09-06 15:09:18 字数 502 浏览 4 评论 0原文

我按照 这篇文章在我的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

森林散布 2024-09-13 15:09:18

处理 404 的最佳指南(我认为)可以在 这个答案。基本上,404 发生的方式有多种:

  1. 不存在路由 - 与 catch all 规则匹配。
  2. 匹配路由但未找到控制器 - 对于具有动态控制器名称的规则 - {controller}/{action}/{parameter} 规则。
  3. 找到路线,但未找到操作 - 通过 HandleUnknownAction 覆盖处理。
  4. 找到路线和操作,但无法转换参数 - 与捕获所有规则匹配。

链接的答案基本上设置了一个控制器,可以从代码中的任何点执行,而无需重写 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:

  1. No route exists - matched by the catch all rule.
  2. Matched route but not found a controller - for rules with dynamic controller names - {controller}/{action}/{parameter} rule.
  3. Found route, but didn't find action - handled through HandleUnknownAction override.
  4. Found route and action but couldn't convert parameters - matched by the catch all rule.

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.

感悟人生的甜 2024-09-13 15:09:18

您是否按照页面底部的建议添加了映射到您的“NotFound”操作的“包罗万象”的路由:

routes.MapRoute("Catch All", "{*path}",
    new { controller = "Error", action = "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:

routes.MapRoute("Catch All", "{*path}",
    new { controller = "Error", action = "NotFound" });

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文