1

I have an MVC 4 application in which the base controller overrides the OnException method as below. Everything works fine in development (IIS Express 8.5) but the method is not called once the app is deployed to production (2012 R2/IIS 8.5). The user sees the standard YSOD error message.

public override void OnException(ExceptionContext filterContext)
{
   //Code here
}

I have confirmed that the web.config files for both applications are identical. The CustomErrors section of the web.config file is set to "On"

   <customErrors mode="On" />

I have compared the application files very carefully and there are no differences in the deployments. I am expecting the issue is in an IIS setting that I cannot seem to locate via Google or by poking around.

Thanks for your help!

tereško
  • 58,060
  • 25
  • 98
  • 150
Eric K
  • 75
  • 1
  • 10
  • I think IISExpress looks at the `` node but full blown IIS looks at ``. Have you checked that `customErrors` are configured underneath that node: http://stackoverflow.com/q/20140674/1224069 – Philip Pittle Aug 15 '14 at 20:09
  • Thanks. I've checked '' and made the settings suggested by Phillip and the referenced post. Still not getting passed to the 'OnException(ExceptionContext filterContext)' method, although everything works fine on IIS Express. All files in the application are identical. The app is running beneath the server root with its own app pool. It appears ASP.NET is handling error (as opposed to IIS), as the error screens that do appear are typical YSOD or Runtime Errors screens we see in ASP.NET all the time. I'm still researching this and appreciate any wisdom. Thanks! – Eric K Aug 19 '14 at 16:50
  • 1
    Breakthrough here. I changed my event logging code in the `OnException(ExceptionContext filterContext)` method and discovered that after making the changes Phillip recommended, the method is actually getting hit. Seems my original event logging routine is bombing on the production server due to permissions issues. Thanks all! – Eric K Aug 19 '14 at 18:54
  • Can you post your working code as an answer? – Philip Pittle Aug 19 '14 at 20:49

1 Answers1

3

An exception raised within the OnException method may cause your application to behave as if that method is not being called. My problematic implementation included a call to save off the error message to a custom Windows error log, but the execution permissions assigned to the IIS application pool's identity where inadequate to create the new event log source. Once I removed the logging logic, the issue resolved. Here are the details. (I have since added the logging logic back once I proved this works).

This is what worked for me:

First, ensure that web.config is configured properly. Two snippets to pay attention to are:

<system.web> <customErrors mode="On" /> </system.web>

and

<system.webServer> 
     <httpErrors errorMode="DetailedLocalOnly" />
</system.webServer>

Second, the following is in my base controller, from which all other controllers in the application inherit:

        protected override void OnException(ExceptionContext filterContext)
        {
            if (filterContext.ExceptionHandled)
            {
                return;
            }

            filterContext.Result = Error(filterContext.Exception.Message,filterContext.Exception.StackTrace);
            filterContext.ExceptionHandled = true;
        }

        public ActionResult Error(string Message, string StackTrace, string ReturnUrl)
        {                 
            ViewBag.ErrorMessage = Message;
            ViewBag.StackTrace = StackTrace;
            ViewBag.ReturnUrl = ReturnUrl;

            return View("~/Views/Shared/MyError.cshtml");
        }
Eric K
  • 75
  • 1
  • 10