[英]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.