I cannot configure IIS to serve my own error pages for errors outside the MVC pipeline. If I throw an exception inside the controller, then the entire file, the Application_Error event, processes the following:
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 go to a non-existent URL, IIS will handle 404 error (or any other error), giving me a 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 the controller inside my application. What can I do to make IIS follow its own error path instead of giving me a standard error page?
This is ASP.NET MVC 3 running on Azure. It also does not work under direct IIS, but the development server runs the controller.