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>
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 :))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With