ASP.NET MVC3 is still looking for Error.aspx / .cshtml, despite specifying my own view

I have an ASP.NET MVC 3 application configured to redirect to an Errors controller with two actions -

 <customErrors mode="On" defaultRedirect="/Errors/ApplicationError"> <error statusCode="404" redirect="/Errors/NotFound" /> </customErrors> 

I have a couple of views that are displayed from each of the action methods:

  • /Views/Shared/NotFound.cshtml
  • /Views/Shared/ApplicationError.cshtml

In my ErrorsController ApplicationError action, I do the following:

 public ActionResult ApplicationError() { Response.StatusCode = 500; return View(); } 

This works, and my view of ApplicationError.cshtml rendered, no matter what happened, anything happened.

However, in ELMAH I see this additional error:

System.InvalidOperationException The Error view or its wizard was not found, or the view mechanism does not support the locations found. The following locations were searched: ~ / Views / Throw / Error.aspx ~ / Views / Throw / Error.ascx ~ / Views / Shared / Error.aspx ~ / Views / Shared / Error.ascx ~ / Views / Throw / Error. cshtml ~ / Views / Throw / Error.vbhtml ~ / Views / Shared / Error.cshtml ~ / Views / Shared / Error.vbhtml

Why is ASP.NET MVC still looking for these views when I have already handled the error and my own view is successfully positioned and displayed?

Additional Information:

  • I do not use the [HandleError] attribute anywhere

  • I replaced filters.Add(new HandleErrorAttribute()); on Global.asax.cs on filters.Add(new ElmahHandleErrorAttribute()); according to this tip , which is based on this answer to the stack overflow from Atif by ELMAH .

+4
source share
1 answer

I think although you have replaced HandleErrorAttribute with a derived class ( ElmahHandleErrorAttribute ), your OnException custom attribute probably still throws base.OnException - i.e. HandleErrorAttribute.OnException.

HandleErrorAttribute.OnException completely ignores the web.config settings (except that ASP.NET displays the standard YSOD if customErrors are disabled) and sets the action result to ViewResult with a status code of 500 and View set to Error (by default, when the View property was not set in the attribute).

Ie, I suppose something like this happens:

  • You have selected an exception
  • ElmahHandleErrorAttribute selects, logs an exception, throws a base.OnException
  • HandleErrorAttribute.OnException sets the result of the ViewResult action with the View set to Error
  • ViewEngine cannot find this view "Error" and therefore throws a new exception
  • ... which is registered by ELMAH but not caught by ElmahHandleErrorAttribute (as this exception occurred outside the scope)
  • Instead, ASP.NET selects it itself and uses errorPages in web.config
  • ... call / Errors / ApplicationError
  • ... which displays the view you want

... but not really the exception that you threw, but the exception that the ViewEngine throws.

You might want to implement your own exception handling rather than base it on HandleError if you want the web.config settings to be executed. Take a look at HandleErrorAttribute in MVC sources ( here ) to find out what needs to be done (and what should not be done in your case).

+3
source

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


All Articles