Wednesday 8 September 2010

MVC custom error problem on IIS7



From bitter experience I have now learned (once again) how important it is to test your applications in a staging environment which is different from your development machine.

In this case our problem was customErrors, which looks fantastic on your developer machine, but gets thrown away when you install your application in the IIS 7 server.

Luckily I found this guys post:
IIS 7 Error Pages taking over 500 Errors which describes the problem. Only when you use MVC and do some error handling in the controllers, it doesnt arrive to the Application_Error method, as he describes, but instead you should add the "TrySkipIisCustomErrors" in the OnException in each controller.

protected override void OnException(ExceptionContext filterContext)
{
string viewName = "MyErrorView";
System.Web.HttpContext.Current.Response.TrySkipIisCustomErrors = true;
View(viewName, errorData).ExecuteResult(this.ControllerContext);

}

In any case its always good to do something in the Application_Error method, if nothing else, log any errors which is not handled any other places