简体   繁体   English

Global.asax中的Application_Error自定义错误处理程序

[英]Application_Error custom error handler in Global.asax

I want to detect and redirect custom errors from Global.asax. 我想从Global.asax中检测并重定向自定义错误。 It returns 200 ok or null. 返回200 OK或为空。 I am not sure how can i detect and redirect to errors automatically. 我不确定如何自动检测并重定向到错误。 I hope you guys have an idea. 我希望你们有一个主意。

In Web.Config 在Web.Config中

<system.web>
    <customErrors mode="On" />
</system.web>
<system.webServer>
    <httpErrors existingResponse="PassThrough"/>
</system.webServer>

In Global.asax 在Global.asax中

protected void Application_Error(object sender, EventArgs e)
{
    Response.TrySkipIisCustomErrors = true;

                Exception ex = Server.GetLastError();

                if (ex is HttpException && ((HttpException)ex).GetHttpCode() == 200)
                {
                    errormessage = ex.Message;
                    Response.RedirectToRoute("error");
                }
                else if (ex is HttpException && ((HttpException)ex).GetHttpCode() == 400)
                {
                    errormessage = ex.Message;
                   Response.RedirectToRoute("error");
                }
                else if (ex is HttpException && ((HttpException)ex).GetHttpCode() == 404)
                {
                    errormessage = ex.Message;
                    Response.RedirectToRoute("error");
                }
                else if (ex is HttpException && ((HttpException)ex).GetHttpCode() == 500)
                {
                    errormessage = ex.Message;
                    Response.RedirectToRoute("error");
                }
                else
                {
                    errormessage = ex.Message;
                    Response.RedirectToRoute("error");
                }
    Server.ClearError();
}

Well, 好,

I found temporary solution for my problem. 我为我的问题找到了临时解决方案。 It's not the best but you may need it. 这不是最好的,但您可能需要它。

In Web.Config 在Web.Config中

<system.web>
    <customErrors mode="On" /> // On or Off doesn't matter ISS 7 or newer version
</system.web>
<system.webServer>
    <httpErrors existingResponse="Auto">
     <clear/>
     <error statusCode="404" path="/notfound.aspx" responseMode="ExecuteURL" />
     </httpErrors>
</system.webServer>

In Global.asax 在Global.asax中

HttpException httpexception = Server.GetLastError().GetBaseException() as HttpException;
string ex_message = httpexception.InnerException;
// send or save errors here
Server.ClearError();
Response.Redirect("error");  

Note: I didn't handle 404 error from codebehind, web.configuration helps about it. 注意:我没有从代码背后处理404错误,web.configuration可以帮助解决这个问题。

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

相关问题 在Global.asax文件中创建Application_Error处理程序,获取解析器错误 - Creating Application_Error handler in Global.asax file getting a Parser Error 我应该抛出Global.asax中的Application_Error处理程序中发生的异常吗? - Should I throw out an exception that happen in Application_Error handler in Global.asax? Starts..c中的Global.asax Application_Error等价物 - Global.asax Application_Error equivalent in Startup.cs Global.asax Application_Error NullReference 异常 - Global.asax Application_Error NullReference Exception global.asax 中的 Application_Error 未捕获 WebAPI 中的错误 - Application_Error in global.asax not catching errors in WebAPI Global.asax Application_Error无法在IIS上触发 - Global.asax Application_Error not firing on IIS global.asax Application_Error 在原始页面上报告 - global.asax Application_Error reporting on original page ASP.NET MVC自定义错误处理Application_Error Global.asax? - ASP.NET MVC Custom Error Handling Application_Error Global.asax? 如果打开自定义错误,是否会触发global.asax Application_Error事件? - Is global.asax Application_Error event not fired if custom errors are turned on? 获取引发Global.asax Application_Error中的最后一个错误的网页 - Get web page that threw the last error in Global.asax Application_Error
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM