Application_Error Global.asax 中的自定义错误处理程序
Application_Error custom error handler in Global.asax
我想检测并重定向来自 Global.asax 的自定义错误。它 returns 200 ok 或 null。我不确定如何自动检测并重定向到错误。希望大家出出主意。
在Web.Config
<system.web>
<customErrors mode="On" />
</system.web>
<system.webServer>
<httpErrors existingResponse="PassThrough"/>
</system.webServer>
在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();
}
嗯,
我找到了解决问题的临时方法。它不是最好的,但你可能需要它。
在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>
在Global.asax
HttpException httpexception = Server.GetLastError().GetBaseException() as HttpException;
string ex_message = httpexception.InnerException;
// send or save errors here
Server.ClearError();
Response.Redirect("error");
注意:我没有处理来自代码隐藏的 404 错误,web.configuration 帮忙解决。
我想检测并重定向来自 Global.asax 的自定义错误。它 returns 200 ok 或 null。我不确定如何自动检测并重定向到错误。希望大家出出主意。
在Web.Config
<system.web>
<customErrors mode="On" />
</system.web>
<system.webServer>
<httpErrors existingResponse="PassThrough"/>
</system.webServer>
在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();
}
嗯,
我找到了解决问题的临时方法。它不是最好的,但你可能需要它。
在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>
在Global.asax
HttpException httpexception = Server.GetLastError().GetBaseException() as HttpException;
string ex_message = httpexception.InnerException;
// send or save errors here
Server.ClearError();
Response.Redirect("error");
注意:我没有处理来自代码隐藏的 404 错误,web.configuration 帮忙解决。