![](/img/trans.png)
[英]Creating Application_Error handler in Global.asax file getting a Parser Error
[英]Application_Error custom error handler in Global.asax
我想從Global.asax中檢測並重定向自定義錯誤。 返回200 OK或為空。 我不確定如何自動檢測並重定向到錯誤。 我希望你們有一個主意。
在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可以幫助解決這個問題。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.