Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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>

IIS default error page

/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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
650 views
Welcome To Ask or Share your Answers For Others

1 Answer

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share

548k questions

547k answers

4 comments

86.3k users

...