Asp.net mvc Asp.NETMVC:处理错误视图中抛出的错误
我在MVC(4)中设置了错误处理,它工作得很好。我已经在global.asax中注册了HandleErrorAttribute,并在web.config中设置了适当的配置。Asp.net mvc Asp.NETMVC:处理错误视图中抛出的错误,asp.net-mvc,Asp.net Mvc,我在MVC(4)中设置了错误处理,它工作得很好。我已经在global.asax中注册了HandleErrorAttribute,并在web.config中设置了适当的配置。 但是,如果我重定向到错误视图,并且错误视图本身抛出错误,我将被重定向回错误页面-无休止。错误发生在应用程序外部管理的布局和布局中。如果布局有错误,我会被套住。我怎样才能防止这种情况?我应该使用哪种类型的错误处理回退?使用不同的布局不是一个选项。我是这样做的。试一试: protected void Application_Er
但是,如果我重定向到错误视图,并且错误视图本身抛出错误,我将被重定向回错误页面-无休止。错误发生在应用程序外部管理的布局和布局中。如果布局有错误,我会被套住。我怎样才能防止这种情况?我应该使用哪种类型的错误处理回退?使用不同的布局不是一个选项。我是这样做的。试一试:
protected void Application_Error(object sender, EventArgs e)
{
//Retrieving the last server error
var exception = Server.GetLastError();
//Erases any buffered HTML output
Response.Clear();
//Declare the exception
var httpException = exception as HttpException;
var routeData = new RouteData();
routeData.Values.Add("controller", "Error"); //Adding a reference to the error controller
if (httpException == null)
{
routeData.Values.Add("action", "ServerError"); //Non HTTP related error handling
}
else //It's an Http Exception, Let's handle it.
{
switch (httpException.GetHttpCode())
{
//these are special views to handle each error
case 401:
case 403:
//Forbidden page.
routeData.Values.Add("action", "Forbidden");
break;
case 404:
//Page not found.
routeData.Values.Add("action", "NotFound");
break;
case 500:
routeData.Values.Add("action", "ServerError");
break;
default:
routeData.Values.Add("action", "Index");
break;
}
}
//Pass exception details to the target error View.
routeData.Values.Add("message", exception);
//Clear the error on server.
Server.ClearError();
//Avoid IIS7 getting in the middle
Response.TrySkipIisCustomErrors = true;
// Call target Controller and pass the routeData.
IController errorController = new ErrorController();
errorController.Execute(new RequestContext(
new HttpContextWrapper(Context), routeData));
}
我是这样做的。试一试:
protected void Application_Error(object sender, EventArgs e)
{
//Retrieving the last server error
var exception = Server.GetLastError();
//Erases any buffered HTML output
Response.Clear();
//Declare the exception
var httpException = exception as HttpException;
var routeData = new RouteData();
routeData.Values.Add("controller", "Error"); //Adding a reference to the error controller
if (httpException == null)
{
routeData.Values.Add("action", "ServerError"); //Non HTTP related error handling
}
else //It's an Http Exception, Let's handle it.
{
switch (httpException.GetHttpCode())
{
//these are special views to handle each error
case 401:
case 403:
//Forbidden page.
routeData.Values.Add("action", "Forbidden");
break;
case 404:
//Page not found.
routeData.Values.Add("action", "NotFound");
break;
case 500:
routeData.Values.Add("action", "ServerError");
break;
default:
routeData.Values.Add("action", "Index");
break;
}
}
//Pass exception details to the target error View.
routeData.Values.Add("message", exception);
//Clear the error on server.
Server.ClearError();
//Avoid IIS7 getting in the middle
Response.TrySkipIisCustomErrors = true;
// Call target Controller and pass the routeData.
IController errorController = new ErrorController();
errorController.Execute(new RequestContext(
new HttpContextWrapper(Context), routeData));
}
当视图中出现错误时,您能否调试并验证
Application\u error
是否在全局中命中。asax?当视图中出现错误时,您能否调试并验证Application\u error
是否在全局中命中。asax?我花了一个下午的时间找出一个bug,直到发现Response.TrySkipIisCustomErrors=true代码>修复。感谢您的回复,艾哈迈德!如果您的ServerError操作引发异常,会发生什么情况?对Graham说:我也遇到过这种情况:)我花了一个下午找出了一个bug,直到我发现Response.tryskipiiscustomerors=true代码>修复。感谢您的回复,艾哈迈德!如果您的ServerError操作引发异常,会发生什么情况?对Graham说:我也遇到过这种情况:)