2

In my asp.net mvc 3 application I've done some error handling and I happy it working. It works properly on the local computer. But on the hosting the iis perhaps substitute errors views by its own error pages. I think and I'm almost sure it's because of Response.TrySkipIisCustomErrors.

But I set Response.TrySkipIisCustomErrors to true; and there's no effect.

web.config

<customErrors mode="Off"></customErrors>

Global.asax

void Application_Error(object sender, EventArgs e)
 {
   var exception = Server.GetLastError();
   Response.Clear();
   Server.ClearError();
   Response.StatusCode = (int)HttpStatusCode.InternalServerError;
   Response.TrySkipIisCustomErrors = true;

   var routeData = new RouteData();
   routeData.Values["controller"] = "Errors";
   routeData.Values["action"] = "ServerError";

   var httpException = exception as HttpException;
   if (httpException != null)
    {
       Response.StatusCode = httpException.GetHttpCode();
       switch (Response.StatusCode)
        {
          case 403:
            routeData.Values["action"] = "Unathorized";
                       break;
          case 404:
            routeData.Values["action"] = "NotFound";
                        break;
                }
       }

 IController errorsController = new ErrorsController();
 var rc = new RequestContext(new HttpContextWrapper(Context), routeData);
 errorsController.Execute(rc);
}

How rewrite this code to get IIS on the hosting handle errros as I want.

Alexandre
  • 13,030
  • 35
  • 114
  • 173
  • imho you wont get much answers since that's not the proper way to handle errors in MVC. I struggled a lot with a similar solution before I switched to proper error handling as I've just described here: http://blog.gauffin.org/2011/11/how-to-handle-errors-in-asp-net-mvc/ – jgauffin Nov 08 '11 at 13:46
  • 1
    Check my answer here: http://stackoverflow.com/questions/6033681/how-to-override-httperrors-section-in-web-config-asp-net-mvc3/8403396#8403396 – bobek Dec 07 '11 at 16:14

1 Answers1

1

Try to derive from a controller base class, which will override the OnException method, and put all your error handling code there.

Gal V
  • 32
  • 3