You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

.NET(IIS)中区分特定500错误配置自定义错误页的方案咨询

问题场景

我在IIS运行的.NET应用中已配置自定义错误页处理404和500错误:

web.config 配置

<customErrors mode="On" defaultRedirect="/shared/error_pages/500.aspx">
    <error statusCode="404" redirect="/shared/error_pages/404.aspx"/>
    <error statusCode="500" redirect="/shared/error_pages/500.aspx"/>
</customErrors>

Global.asax 错误日志逻辑

Sub Application_Error(ByVal sender As Object, ByVal e As EventArgs)

    Dim ex = Server.GetLastError()
    Dim sessionid = Trim(Request.Cookies("sessionid").Value)
    If (TypeOf ex Is HttpException) Then
        Dim httpex = CType(ex, HttpException)
        Dim httpCode = httpex.GetHttpCode()
        Dim innerEx = httpex.InnerException.Message
        If (httpCode <> 404) Then
            Dim _params As New NameValueCollection
            _params.Add("@edit", 1)
            _params.Add("@page_name", sender.Request.CurrentExecutionFilePath)
            _params.Add("@error_message", httpex.Message)
            _params.Add("@error_stacktrace", httpex.StackTrace)
            _params.Add("@sessionid", sessionid)
            TNXCDataAdapter.Execute("procedure", _params)
        End If
    End If

End Sub

上述代码会将所有非404错误记录到数据源。现在需要精准识别包含“Session is invalid”的特定500错误,让这类错误跳转到web.config定义的500错误页之外的其他页面,请问最高效的实现方式是什么?


最高效实现方案

直接在Global.asax的Application_Error事件中完成判断与重定向,这是错误发生后最早的拦截点,无需额外传递数据,逻辑清晰且性能最优。

核心思路

  1. 捕获到500错误时,检查错误消息(包括内部异常)是否包含“Session is invalid”
  2. 匹配成功后,直接重定向到目标页面,并调用Server.ClearError()清除错误,阻止web.config中customErrors的默认跳转逻辑
  3. 保留原有错误日志逻辑,不影响其他错误的处理流程

修改后的Global.asax代码

Sub Application_Error(ByVal sender As Object, ByVal e As EventArgs)

    Dim ex = Server.GetLastError()
    Dim sessionid = Trim(Request.Cookies("sessionid").Value)
    Dim isSessionInvalidError = False

    If (TypeOf ex Is HttpException) Then
        Dim httpex = CType(ex, HttpException)
        Dim httpCode = httpex.GetHttpCode()
        Dim innerExMsg = If(httpex.InnerException IsNot Nothing, httpex.InnerException.Message, "")
        
        ' 检查错误消息是否包含目标关键词
        isSessionInvalidError = httpCode = 500 AndAlso 
                               (httpex.Message.Contains("Session is invalid") OrElse innerExMsg.Contains("Session is invalid"))

        If (httpCode <> 404) Then
            ' 保留原有错误日志逻辑
            Dim _params As New NameValueCollection
            _params.Add("@edit", 1)
            _params.Add("@page_name", sender.Request.CurrentExecutionFilePath)
            _params.Add("@error_message", httpex.Message)
            _params.Add("@error_stacktrace", httpex.StackTrace)
            _params.Add("@sessionid", sessionid)
            TNXCDataAdapter.Execute("procedure", _params)
        End If
    End If

    ' 如果是Session无效错误,重定向到指定页面并清除错误
    If isSessionInvalidError Then
        Server.ClearError()
        Response.Redirect("/shared/error_pages/session_invalid.aspx") ' 替换为你的目标页面路径
    End If

End Sub

关键说明

  • Server.ClearError()必须调用,否则customErrors的默认跳转逻辑仍会触发,导致两次重定向
  • 同时检查HttpException.Message和InnerException.Message,确保不会遗漏嵌套的错误信息
  • 逻辑顺序:先完成错误日志记录,再处理重定向,保证错误数据不会丢失

内容的提问来源于stack exchange,提问作者DevOps Admin

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.24 05:03:27