简体   繁体   English

开发Web服务器在404上激活Application_Error,为什么不用IIS7?

[英]Development web server fires Application_Error on 404, why doesn't IIS7?

I'm using Application_Error to catch some legacy URLs and URL shortcuts. 我正在使用Application_Error来捕获一些遗留URL和URL快捷方式。 In Global.vb I have this code: 在Global.vb中我有这个代码:

    Sub Application_Error(ByVal sender As Object, ByVal e As EventArgs)
        Dim serverError = TryCast(Server.GetLastError(), HttpException)
        If serverError IsNot Nothing Then
            Dim errorCode As Integer = serverError.GetHttpCode()
            If 404 = errorCode Then
               ' Do some custom processing here
            End If
        End If
    End Sub

In web.config I have this, to ensure that all requests, not just ones ending in .aspx, are handled by aspnet_isapi.dll so I get to process them: 在web.config中我有这个,以确保所有请求,而不仅仅是以.aspx结尾的请求,由aspnet_isapi.dll处理,所以我得处理它们:

        <add name="ASP.NET-ISAPI-2.0-Wildcard" path="*" verb="*" modules="IsapiModule" scriptProcessor="%windir%\Microsoft.NET\Framework\v2.0.50727\aspnet_isapi.dll" resourceType="Unspecified" requireAccess="None" preCondition="classicMode,runtimeVersionv2.0,bitness32" />

On my development box (using Cassini), this works fine in all cases: Both /badurl and /badurl.aspx cause Application_Error to fire. 在我的开发框中(使用Cassini),这在所有情况下都能正常工作:/ badurl和/badurl.aspx都会导致Application_Error触发。

In IIS7, however, /badurl.aspx works as expected, but /badurl just results in a generic server-generated 404 page. 但是,在IIS7中,/ badur.aspx按预期工作,但/ badurl只会导致通用服务器生成的404页面。

Any ideas what causes the difference, and how I can get IIS7 to replicate the development server's behavior? 是什么导致差异的想法,以及我如何让IIS7复制开发服务器的行为?

try to add this to web.config file. 尝试将此添加到web.config文件。

 <customErrors mode="On" defaultRedirect="appError.aspx">
          <error statusCode="403" redirect="appError.aspx"/>
          <error statusCode="404" redirect="appError.aspx"/>
        </customErrors>

You can try in two ways: 你可以尝试两种方式:

  1. in your code you can set Response.TrySkipIisCustomErrors = true; 在您的代码中,您可以设置Response.TrySkipIisCustomErrors = true;
  2. in config file you can set: 在配置文件中,您可以设置:

<customErrors redirectMode="ResponseRewrite mode="On" defaultRedirect="appError.aspx" />

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

相关问题 Global_Asax Application_Error已取消,而不是默认IIS7 404 Page Not Found Page - Global_Asax Application_Error Fired instead of default IIS7 404 Page Not Found Page 为什么我的global.asax中的Application_Error无法捕获“ 500:内部服务器错误”? - Why doesn't Application_Error in my global.asax catch the “500:Internal server error”? Global.asax中的Application_Error事件在IIS7中未触发,但在IIS6中工作正常 - Application_Error event in Global.asax not firing in IIS7, but works fine in IIS6 Application_Error()不会触发 - Application_Error() doesn't fire Web应用程序在VS Development Server中工作但在IIS中不起作用 - Web Application working in VS Development Server but don't work in IIS 移至IIS7服务器后出现Signalr 404错误 - Signalr 404 error after moving to iis7 server Application_Error 未捕获 404 类型的错误 - Application_Error is not catching 404 type of error 为什么不能通过IIS7中的web.config删除“服务器”响应标头? - Why can't the “Server” Response Header be removed via web.config in IIS7? 为什么我的代码在localhost上运行而不在带有IIS7的Web服务器上运行? - Why my Code is working on the localhost but not on a web server with IIS7? 使用IIS7调试而不是开发服务器 - Using IIS7 to debug instead of Development Server
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM