问题描述
我无法设置IIS以服务为MVC管道之外的错误我的自定义错误页。如果我把一个控制器内的异常它的所有文件,Application_Error事件句柄:
I am unable to set IIS to serve my custom error pages for errors outside of the MVC pipeline. If I throw an exception inside a controller it's all file, the Application_Error event handles that:
protected void Application_Error(object sender, EventArgs e)
{
var error = Server.GetLastError();
var routeData = new RouteData();
routeData.Values["Controller"] = "Error";
routeData.Values["Area"] = "";
routeData.Values["Exception"] = error;
if (error is HttpException)
{
switch (((HttpException)error).GetHttpCode())
{
case 401:
routeData.Values["Action"] = "NotAllowed";
break;
case 403:
routeData.Values["Action"] = "NotAllowed";
break;
case 404:
routeData.Values["Action"] = "NotFound";
break;
default:
routeData.Values["Action"] = "ServerError";
break;
}
}
else
{
routeData.Values["Action"] = "ServerError";
}
Response.Clear();
Server.ClearError();
IController controller = new ErrorController();
controller.Execute(new RequestContext(new HttpContextWrapper(((MvcApplication)sender).Context), routeData));
}
但是,如果我浏览到一个不存在的URL,IIS将处理404错误(或任何其他错误),让我有标准的IIS错误消息,并且完全无视我的web.config设置:
However, if I browse to a non-existent URL, IIS will handle the 404 error (or any other error), giving me the standard IIS error message and completely ignoring my web.config settings:
<system.web>
<customErrors mode="On" redirectMode="ResponseRewrite" defaultRedirect="/error">
<error statusCode="401" redirect="/error/notallowed" />
<error statusCode="403" redirect="/error/notallowed" />
<error statusCode="404" redirect="/error/notfound" />
<error statusCode="500" redirect="/error/servererror" />
</customErrors>
</system.web>
<system.webServer>
<httpErrors errorMode="Custom" defaultResponseMode="ExecuteURL">
<clear />
<error statusCode="401" path="/error/notallowed" />
<error statusCode="403" path="/error/notallowed" />
<error statusCode="404" path="/error/notfound" />
<error statusCode="500" path="/error/servererror" />
</httpErrors>
</system.webServer>
/故障/ *由我的应用程序中的控制器处理。我能做些什么使IIS执行,而不是喂我的标准错误页面的自定义错误路径?
/error/* is handled by a controller inside my application. What can I do to make IIS execute the custom error path, instead of feeding me the standard error page?
这是ASP.NET MVC 3,在Azure上运行。它不直接下IIS工作下去,但开发服务器确实执行控制器。
This is ASP.NET MVC 3, running on Azure. It doesn't work under straight IIS either, but the development server does execute the controller.
推荐答案
IIS不尊重defaultResponseMode。您必须添加responseMode到每一个错误,像这样:
IIS does not respect defaultResponseMode. You must add responseMode to each and every error, like so:
<httpErrors errorMode="Custom">
<clear />
<error statusCode="401" path="/error/notallowed" responseMode="ExecuteURL" />
<error statusCode="403" path="/error/notallowed" responseMode="ExecuteURL" />
<error statusCode="404" path="/error/notfound" responseMode="ExecuteURL" />
<error statusCode="500" path="/error/servererror" responseMode="ExecuteURL" />
</httpErrors>
为什么默认情况下不适用我无法理解。
Why default doesn't apply is beyond me.
这篇关于启用Azure中的自定义错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!