The model element passed to the dictionary is of type "System.Web.Mvc.HandleErrorInfo", but this dictionary requires a model element of type

Whenever an error occurs in my application, I cannot see the correct error in the event viewer. At this point, I get the following error ...

The model element passed to the dictionary is of type "System.Web.Mvc.HandleErrorInfo", but this dictionary requires a model element of type "LayoutPageViewModel"

I understand why this error occurs (because the controller is trying to pass a model like HandleErrorInfo to the original view), but I cannot figure out how to stop this error appearing in the event viewer and show the real error.

Thus, the sequence of events:

  • An exception occurs in the application.
  • The default error handling attempts to pass a model of type "System.Web.Mvc.HandleErrorInfo" to the default layout page, which accepts a model of "LayoutPageViewModel"
  • Another exception is thrown in the application because the layout is passed by a model of type "HandleErrorInfo"
  • The page with a custom error 500 (specified in the web.config file) that does not reference any layout is deleted:

    @{ Layout = null; } 
  • The error page displays correctly, but the exception in the event viewer is incorrect.

I tried to install the wizard and view the HandleErrorAttribute filter in Application_Start, but this stops everything that is being logged in the event logs. I also tried adding the following controller to the controller ...

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

but it has the same result as the HandleErrorAttribute workaround.

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

+6
source share
2 answers

It seems like you are experiencing secondary errors, which makes the end result a type problem.

See how you show your exception before trying other ways to handle exceptions.

How do you know that the first page with an error hit the first time?

In what content is the error page included, can anything else cause the error?

You know, you said that on the error page there is no link to the layout page. If you double-checked that it was actually used, and not just called in the second instance, remove the layout from main one by one to make sure.

Make sure that your error page always has its own simplified layout page, so there is no risk of getting a problem due to the strongly typed layout / main page, which will lead to a similar error for yours.

0
source

My solution to solve the problem is to remove the @model directive at the top of the layout page, and then do some checks where I usually expect to see my model switch between different models that can be passed in e.g.

 @if (Model is System.Web.Mvc.HandleErrorInfo) { <title>Error</title> } else if (Model.GetType() == typeof(MyApp.Models.LayoutPageViewModel)) { <meta name="description" content="@Model.PageMetaDescription"> <title>@Model.PageTitleComplete</title> } 
0
source

Source: https://habr.com/ru/post/955668/


All Articles