In my web.config, I have:
<system.web>
<customErrors mode="On" defaultRedirect="Error.cshtml" />
</system.web>
In Views/Shared/Error.cshtml, I have:
@model System.Web.Mvc.HandleErrorInfo
@{
ViewBag.Title = "Error";
}
<h2>
Sorry, an error occurred while processing your request.
</h2>
If I put an invalid URL/route into my browser, I get this:
Server Error in ‘/’ Application.
The resource cannot be found.
Description: HTTP 404. The resource you are looking for (or one of its dependencies) could have been removed, had its name changed, or is temporarily unavailable. Please review the following URL and make sure that it is spelled correctly.
Requested URL: /Error.cshtml
Why can’t Error.cshtml be found? The file is definitely in Views/Shared.
The defaultRedirect won’t go directly to a view. Your defaultRedirect looks like a razor view file which it can’t process. For example: Where does it get the model from? It isn’t, and can’t, be specified in the config file so it can’t process a view.
If you want more dynamic error pages in MVC you might want to read custom error pages and error handling in MVC 3