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

asp.net webforms handle 404 notfound error in codebehind?

只是想检测它是否是应用程序中的 404 错误。例如,用户尝试 url 并且 IIS 显示 404 未找到。

我想检测 404 错误。重定向到自定义 404 页面并从代码隐藏动态传递消息,如 "cound't find"。

有人做过吗?

使用web.config自定义错误处理404,这是处理此类错误的正确方法,您可以在NotFound.aspx页面中记录错误。

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

无法处理来自代码隐藏的所有 404 异常,因为异常的通用处理程序可以在 global.asax -(Application_Error 方法)中实现,此处理程序能够捕获发生在 asp.net 应用层,无法处理 404 请求,因为您正在请求 asp.net.

之外的内容