Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
661 views
in Technique[技术] by (71.8m points)

asp.net mvc - Enable custom errors in Azure

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

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

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...