提问人:MatthewNYC 提问时间:5/10/2022 更新时间:5/10/2022 访问量:349
如何禁用 IIS 错误页面并处理 Global.asax 中的错误?
How Do I Disable IIS Error Pages And Handle Errors In Global.asax?
问:
我一直在尝试禁用IIS错误处理,以便可以捕获Global.asax中的所有错误。
我正在运行 Windows 2012 和 IIS 版本 6.2。
例如,当我导航到丢失的图像文件时,例如:
www.mysite.com/thisimagesdoesntexist.jpg
我收到 IIS 错误页面,但我的 Global.asax 没有捕获错误。
我尝试添加到我的web.config:
<system.webServer>
<httpErrors existingResponse="Passthrough" />
</system.webServer>
而且,在 Global.asax 中:
Server.ClearError()
Response.TrySkipIisCustomErrors = True
但是,缺少图像错误仍由 IIS 处理。下面是我的 web.config 和 Global.asax 例程的代码:
<?xml version="1.0"?>
<configuration>
<appSettings>
<!--Unobtrusive validation used JQuery-->
<add key="ValidationSettings:UnobtrusiveValidationMode" value="WebForms" />
</appSettings>
<system.web>
<httpRuntime targetFramework="4.6.1" />
<!--Enable Sessions and don't scroll a form when doing a postback-->
<!-- clientIDMode="Static" ensures the ASP.net controls have the exact name specified in their ID tags
This is important so CSS targets the correct IDs for styling. -->
<pages enableSessionState="true" controlRenderingCompatibilityVersion="3.5" clientIDMode="Static" maintainScrollPositionOnPostBack="true" />
<!--Use cookies if enabled; use querystring if disabled-->
<!--<sessionState mode="InProc" cookieless="AutoDetect" timeout="20" />-->
<!-- cookieless="AutoDetect" is adding a cookie parameter to the URL that was causing FastSpring posts to fail -->
<sessionState mode="InProc" timeout="20" />
<!--Show detailed errors on the website while developing-->
<customErrors mode="Off" />
<compilation debug="true" targetFramework="4.6.1" />
<!--Use Tracing-->
<trace enabled="true" pageOutput="false" requestLimit="40" localOnly="false" />
<globalization fileEncoding="utf-8" requestEncoding="utf-8" responseEncoding="utf-8" culture="auto" uiCulture="auto" />
</system.web>
<!--
<system.webServer>
<httpErrors errorMode="Detailed" />
</system.webServer>
-->
<system.webServer>
<httpErrors existingResponse="Passthrough" />
</system.webServer>
<system.codedom>
<compilers>
<compiler extension=".cs" language="c#;cs;csharp" warningLevel="4" compilerOptions="/langversion:7.0 /nowarn:1659;1699;1701;612;618" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.CSharpCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=3.6.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
<compiler extension=".vb" language="vb;vbs;visualbasic;vbscript" warningLevel="4" compilerOptions="/langversion:default /nowarn:41008,40000,40008 /define:_MYTYPE=\"Web\" /optionInfer+" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.VBCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=3.6.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
</compilers>
</system.codedom>
</configuration>
Sub Application_Error(ByVal sender As Object, ByVal e As EventArgs)
Dim Uri As Uri = New Uri(Request.Url.AbsoluteUri)
Dim ex As HttpException = CType(Server.GetLastError(), HttpException)
Dim httpCode As Integer = ex.GetHttpCode()
Dim strErrorDescription As String = ex.Message.ToString
Server.ClearError()
Response.TrySkipIisCustomErrors = True
If httpCode = 500 Then
Response.Redirect("~/error.aspx?type=globalerror&errdescription=" & HttpUtility.UrlEncode(strErrorDescription) & "&url=" & HttpUtility.UrlEncode(Uri.ToString))
ElseIf httpCode = 404 Then
Response.Redirect("~/404.aspx?type=globalhttp&errdescription=" & HttpUtility.UrlEncode(strErrorDescription) & "&url=" & HttpUtility.UrlEncode(Uri.ToString))
Else
'Let's see what other errors are picked up and logged. Perhaps we log these to a separate table?
Response.Redirect("~/error.aspx?type=globalhttp&errdescription=" & HttpUtility.UrlEncode(strErrorDescription) & "&url=" & HttpUtility.UrlEncode(Uri.ToString))
End If
End Sub
答: 暂无答案
评论