5

Background: On a public facing ASP.NET MVC4 application, sometimes I receive inbound requests to a bad URL. The referrer is from outside so my app which is out of my control (I'm not generating the bad URL in my app). So MVC correctly raises an exception AND the user sees the custom error page. The global.asax is coded to email errors to me.

Problem. Although the URL is bad, the error I'm receiving is unexpected.

Ex: - User navigates (from an external URL) to /Blog/View - The Blog controller does not have a View action - The user is presented with the Error500 custom error web page - The error I receive by email is:

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

I don't understand why ASP.NET MVC4 is looking for a view named "Error", and why MVC does not search for the view as specified (Error500) in the web.config. Here are the applicable source files:

The Web.Config:

<customErrors mode="RemoteOnly" defaultRedirect="~/Error/Error500">
  <error statusCode="404" redirect="~/Error/Error404" />
</customErrors>

The ErrorController file:

   public class ErrorController : Controller
   {
       public ActionResult Error500()
       {
           return View();
       }

       public ActionResult Error404()
       {
          return View();
       }
   }

The Error404.cshtml file (located in the /Views/Error folder):

   @{
       ViewBag.Title = "Oops...";
       Layout = "~/Views/Shared/_Layout.cshtml";
   }

   <h1>That's interesting</h1>
   <p>The page you were looking for could not be found.</p>

The Error500.cshtml file (located in the /Views/Error folder):

@{
    ViewBag.Title = "Oops...";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<h1>We're sorry about that</h1>
<p>Something unexpected just happened.  Our IT staff has been notified...time to code a hot-fix!</p>
Erik Olson
  • 5,807
  • 1
  • 18
  • 17
  • check this out http://stackoverflow.com/questions/10199345/the-view-error-or-its-master-was-not-found – Bassam Mehanni Feb 01 '13 at 17:05
  • 1
    Hi Bassam. Thanks for the link. I read through that question and the answers several times before posting and just now. I don't think it applies because I'm not having any issue with the global's Application_Error, I'm not using Elmah, and I don't have any rouge references to an Error action or view that I can find. It just appears the MVC is trying find an Error when I haven't specified that it should. I believed I've followed the typical convention, but MVC is not rendering my /Views/Error/Error500 view. – Erik Olson Feb 01 '13 at 20:59
  • @ErikOlson were you able to figure out this issue? I'm facing the same issue. – Ram Apr 14 '21 at 21:15

1 Answers1

2

Are you sure that the user is hitting a URL that does not exist? It seems from a small repro that I just made that this works as desired if the action method does not exist. When an error is thrown inside an action method that was resolved by the routing and you are using the HandleErrorAttribute then you will get the error that you mention. What does your routing look like?

As an aside emailing your self on error is not a sustainable route to receiving errors about your application! You should look into an error logging service to handle this for you. I recommend Bugsnag. (Disclaimer: I work at Bugsnag :))

martin308
  • 706
  • 6
  • 20