4

I use the following code in Global.asax.cs in order to log unhandled errors and transfer user to one of the error pages. Error pages are implemented as standard MVC controllers/views.

        protected void Application_Error(Object sender, EventArgs e)
    {
        Exception ex = HttpContext.Current.Server.GetLastError();

        //Handle errors

        var httpException = ex as HttpException;
        Response.Clear();
        Server.ClearError();
        var routeData = new RouteData();
        routeData.Values["controller"] = "Errors";
        routeData.Values["action"] = "Index";
        routeData.Values["exception"] = ex;
        Response.StatusCode = 500;
        if (httpException != null)
        {
            Response.StatusCode = httpException.GetHttpCode();
            switch (Response.StatusCode)
            {
                case 403:
                    routeData.Values["action"] = "Http403";
                    break;
                case 404:
                    routeData.Values["action"] = "Http404";
                    log.Error("Error 404 - User sees page not found page", ex);
                    break;
            }
        }
        log.Fatal("USER SEES ERROR 500 PAGE", ex);
        IController errorsController = new errorsController();
        var rc = new RequestContext(new HttpContextWrapper(Context), routeData);
        errorsController.Execute(rc);
    }

It worked fine on the development machine (Windows 7 Pro). However, when I moved the site to Windows Server 2008 R2, the errors page stopped showing. Instead, I see standard IIS error pages. Yet, errors are logged properly.

What am I doing wrong? Thank you.

2
  • Check to make sure the CustomErrors section in your web config file has not been jacked with.
    – Code Jammr
    Dec 3, 2011 at 4:27
  • Should I enable or disable custom errors for this case? Dec 3, 2011 at 10:58

1 Answer 1

1

Make sure that you have removed the HandleErrorAttribute global attribute registration from Global.asax. Or if you are using ASP.NET MVC 2 remove the [HandleError] attribute from your controllers. Also try setting errorMode to Detailed:

<system.webServer>
    <httpErrors errorMode="Detailed" />
</system.webServer>
3
  • I removed it, but this behavior did not seem to change. Dec 3, 2011 at 10:58
  • @AlexAvrutin, try setting <httpErrors errorMode="Detailed" /> in your web.config. Dec 3, 2011 at 11:03
  • Thank you, it helped! What does this line change? Dec 3, 2011 at 22:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.