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));
}
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>
/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?
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.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…