简体   繁体   中英

Can't find an Error page although an Error view exists ASP.NET MVC

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?

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!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM