如 Application_Error
由应用程序启动时的异常触发,即 RouteConfig
或 BundleConfig
如何检查 Request
/Response
可用吗?目前调用Response.Clear
抛出 System.Web.HttpException
附加信息 Response is not available in this context
.
void Application_Error(object sender, EventArgs e)
{
//Log error
Log.Error(e);
//Clear
Response.Clear();
Server.ClearError();
//Redirect
Response.Redirect("~/Error");
}
其他问题建议改写不使用
Response
或使用 HttpContext.Current.Response
或更改 IIS config .总之;如何判断应用程序启动期间是否发生错误?
最佳答案
你要检查是否是 HttpException .
protected void Application_Error(Object sender, EventArgs e)
{
var exception = Server.GetLastError();
// Log error
LogException(exception);
var httpException = exception as HttpException;
if (httpException != null)
{
Response.Clear();
Server.ClearError();
Response.TrySkipIisCustomErrors = true;
Response.Redirect("~/Error");
}
}
关于asp.net - 如何判断 Application_Error 中的请求/响应是否可用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28092689/