简体   繁体   English

asp.net webforms 处理代码隐藏中的 404 notfound 错误?

[英]asp.net webforms handle 404 notfound error in codebehind?

Just trying to detect if it's 404 error in application.只是试图检测它是否是应用程序中的 404 错误。 For example user tries in url and IIS shows 404 not found.例如,用户尝试输入 url 并且 IIS 显示 404 not found。

I want to detect 404 error.我想检测 404 错误。 Redirect to custom 404 page and pass message like "cound't find" dynamically from the codebehind.重定向到自定义 404 页面并从代码隐藏动态传递诸如“找不到”之类的消息。

Has anyone ever done that?有没有人这样做过?

Handle 404 using web.config custom errors, this is the correct way to handle this type of errors, and in NotFound.aspx page you can log the error.使用 web.config 自定义错误处理 404,这是处理此类错误的正确方法,您可以在 NotFound.aspx 页面中记录错误。

<configuration>
  <system.web>
    <customErrors mode="On" >
      <error statusCode="404" redirect="NotFound.aspx"/>
    </customErrors>
  </system.web>
</configuration

Noway to handle all 404 exceptions from code behind, because the generic handler of exception could be implemented in global.asax - (Application_Error method), this handler able to catch any error occurred on asp.net application layer and cannot handle 404 request because you are request something outside of asp.net.没有办法从后面的代码中处理所有 404 异常,因为异常的通用处理程序可以在 global.asax 中实现 - (Application_Error 方法),这个处理程序能够捕获任何发生在 asp.net 应用程序层的错误,并且无法处理 404 请求,因为您请求asp.net 之外的东西。

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

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