设定:
IIS错误的web.config设置如下:
<httpErrors errorMode="Custom" existingResponse="Replace" defaultPath="/StaticErrors/Default.html" defaultResponseMode="ExecuteURL">
<clear />
<error statusCode="400" path="/myCustomErrors/Http400" responseMode="ExecuteURL" />
<error statusCode="401" path="/myCustomErrors/Http401" responseMode="ExecuteURL" />
<error statusCode="403" path="/myCustomErrors/Http403" responseMode="ExecuteURL" />
<error statusCode="404" path="/myCustomErrors/Http404" responseMode="ExecuteURL" />
<error statusCode="500" path="/myCustomErrors/Http500" responseMode="ExecuteURL" />
</httpErrors>
这通常正常,但是,如果我添加以下customErrors代码 - 那么任何错误,例如错误请求或未找到将总是导致错误500页面显示。由于某种原因,HttpException被视为未处理的异常。
<customErrors defaultRedirect="/StaticErrors/Default.html" mode="On" redirectMode="ResponseRewrite">
<error redirect="~/myCustomErrors/Http400" statusCode="400" />
<error redirect="~/myCustomErrors/Http401" statusCode="401" />
<error redirect="~/myCustomErrors/Http403" statusCode="403" />
<error redirect="~/myCustomErrors/Http404" statusCode="404" />
<error redirect="~/myCustomErrors/Http500" statusCode="500" />
</customErrors>
在仅限MVC的应用程序中以这种方式禁用ASP.NET错误处理会影响哪些问题。
答案 0 :(得分:0)
最后,我只能通过使用Global.asax文件和Application_Error方法有效地重写默认错误处理容量来解决这个问题。
protected void Application_Error()
{
var errorCodeToUse = 500;
var currentException = Server.GetLastError();
if (currentException != null)
{
var httpException = currentException as System.Web.HttpException;
if (httpException != null)
{
errorCodeToUse = httpException.GetHttpCode();
}
}
var currentContext = HttpContext.Current;
RequestContext requestContext = null;
if (currentContext.CurrentHandler != null)
{
requestContext = ((MvcHandler)currentContext.CurrentHandler).RequestContext;
}
if (requestContext == null)
{
requestContext = new RequestContext(new HttpContextWrapper(Context), new RouteData());
}
requestContext.RouteData.Values["controller"] = "Error";
switch (errorCodeToUse)
{
case 400:
requestContext.RouteData.Values["action"] = "Http400";
break;
case 401:
requestContext.RouteData.Values["action"] = "Http401";
break;
case 403:
requestContext.RouteData.Values["action"] = "Http403";
break;
case 404:
requestContext.RouteData.Values["action"] = "Http404";
break;
case 405:
requestContext.RouteData.Values["action"] = "Http405";
break;
case 429:
requestContext.RouteData.Values["action"] = "Http429";
break;
default:
requestContext.RouteData.Values["action"] = "Http500";
break;
}
var factory = ControllerBuilder.Current.GetControllerFactory();
var controller = factory.CreateController(requestContext, "Error");
controller.Execute(requestContext);
currentContext.Server.ClearError();
}