2

In an ASP.NET MVC web application I'm working on I am showing na Error view when there is an uncaught error in the application. In the Web.config:

<customErrors mode="On" defaultRedirect="~/Error" />

Controller:

 public class ErrorController : BaseController
{
    public ActionResult Index()
    {
        return View();
    }
}

And I have a view in folder Views/Error, Index.cshtml. Its content is not important for the issue I'm facing.

When there is for example an error 404 everything works OK - the Error Index.cshtml page is shown(with the URL http://localhost:51099/Error?aspxerrorpath=/Home/ssad, which is OK) and in the application's error log the 404 error is reported. But when there is some other error, for example when I try to create a file in non-existing folder through my application, the Error Index.cshtml view is shown(with the URL http://localhost:51099/Error?aspxerrorpath=/AdminDataExport/ResponseMatrix, which is OK) but there are two errors reported - one that the folder cannot be found which is OK, and the other is:

The view 'Error' or its master was not found or no view engine supports 
the searched locations. The following locations were searched:
~/Views/AdminDataExport/Error.aspx
~/Views/AdminDataExport/Error.ascx
~/Views/Shared/Error.aspx
~/Views/Shared/Error.ascx
~/Views/AdminDataExport/Error.cshtml
~/Views/AdminDataExport/Error.vbhtml
~/Views/Shared/Error.cshtml
~/Views/Shared/Error.vbhtml

Why does the application search for an Error file in this locations, when I have an ErrorController and a corresponding view defined? Why does this second error appear in some cases and in some don't?

I want to fix the second error. I tried fixing it with adding an Error.cshtml file in the Views/Shared. The result is that the second error is not being logged which is what I want, but the URL is different from the first error - http://localhost:51099/AdminDataExport/ResponseMatrix and I want it to be http://localhost:51099/Error?aspxerrorpath=/AdminDataExport/ResponseMatrix. Is this possible?

kiriz
  • 655
  • 1
  • 7
  • 24
  • If you are writing the code now and can work with other approaches, check my answer in this question, it may help -> http://stackoverflow.com/questions/26357340/asp-mvc-handleerror-attribute-doesnt-work/26359455#26359455 – SBirthare Nov 06 '14 at 11:21
  • @SBirthare I implemented your solution and I have two questions. One is on the line `controller.ViewData.Model = new HandleErrorInfo(ex, currentController, currentAction);`. On the "ex" I get a warning "Possible 'null' assignment to entity marked with 'NotNull' attribute". Second is that when I produce an error it gives me a source code(HTML) of my error page, not the rendered page. Have you had any similar experience? How to solve this situation? – kiriz Nov 06 '14 at 14:24
  • I did not added extra check for ex in this line, have not tested for that condition. You can add a check. On second question, this method invoke an action on ErrorController based on error code. You can decide what to return from that action. In my case i return corresponding view. Hope this help. – SBirthare Nov 06 '14 at 14:48
  • On you second question, I get to see proper page based on error. It is working in production too so something different in our setup. Can you add more information (in a separate Q may be) around what's happening after the implementation I may be able to help. – SBirthare Nov 06 '14 at 14:58
  • @SB There is not much to tell...Your solution is rendering the page well when the error is not 404. When the error is 404 I get literally a Page Source displayed on the browser...And I put for all errors to go to Error/Index. – kiriz Nov 06 '14 at 15:50
  • I'm having this same exact problem...it's driving me nuts! – matt_dev Feb 05 '15 at 20:27

1 Answers1

0

Ah Ha!

Found the answer...

MVC Adds the FilterConfig class with the following code.

        public static void RegisterGlobalFilters(GlobalFilterCollection filters)
        {
            filters.Add(new HandleErrorAttribute());
        }

Just remove it and the errors are gone!

matt_dev
  • 5,176
  • 5
  • 33
  • 44