C# – The model item passed into the dictionary is of type ‘System.Web.Mvc.HandleErrorInfo’, but this dictionary requires a model item of type

asp.net-mvcasp.net-mvc-4c

Whenever an error occurs in my application, I'm not able to view the correct error in the event viewer. In it's place I get the following error…

The model item passed into the dictionary is of type 'System.Web.Mvc.HandleErrorInfo', but this dictionary requires a model item of type 'LayoutPageViewModel'

I get why this error occurs (because the controller is trying to pass a model of type HandleErrorInfo to the original view) but what I can't figure out is how stop this error showing up in the event viewer and show the real error.

So the sequence of events are:

  1. Exception occurs in the application
  2. Default error handling tries to pass model of type 'System.Web.Mvc.HandleErrorInfo' into default layout page, which accepts a model of 'LayoutPageViewModel'
  3. Another exception occurs in the application because the layout is being passed a model of type 'HandleErrorInfo'
  4. The custom error 500 page (specified in the web.config) is hit, which doesn't reference any layout:

    @{ Layout = null; }
    
  5. Error page is shown correctly but the exception in the event viewer is incorrect.

I have tried setting the master and view for the HandleErrorAttribute filter in Application_Start but that stops anything being registered in the event logs. I've also tried adding the following method to the controller…

protected override void OnException(ExceptionContext filterContext)
{
    filterContext.Result = new ViewResult {
        ViewName = "~/Views/Shared/Error.cshtml",                
    };
}

but that has the same result as the HandleErrorAttribute workaround.

Does anyone have an idea of how I can get around this problem?

Best Answer

Sounds like you are experiencing secondary errors which makes the end result a type based issue.

Would look at the way you display your exception before trying other ways to handle exceptions.

How do you know that the right error page is hit first time?

What content does the error page include, can anything else trigger an error?

Know you said the error page has no ref to layout page. Would double check that this is actually being used and not just being called in second instance, alternately remove layout from main to ensure.

Make sure that your error page always has its own simplified layout page so there is not risk to get a problem due to a strongly typed layout/master page which will result in a similar error to yours.

Related Topic