Introduction
So, whenever an error occurs, you would like to show a custom 404 error page in ASP.NET MVC? This blog post describes how to techically handle and display error page effectively. Reader should have some basic knowledge of setting IIS and ASP .NET MVC project.
Error page is displayed when something goes wrong. Page should inform user about what happened and what he or she should do next. Page should also inform bots, crawlers, spiders not to search any relevent content in there, e.g. Google won’t display error page as search result. This is handled with HTTP status code known as 404. There are also other HTTP status codes for specific problems, e.g. for maintenance, for restricted access…
Create action and view
First, create a friendly page for user and set indicator for bots not to handle this page as obvious page. In this case “Main” controller with “Error” action. Note for IE 5,6: error page should have content larger than 512 bytes otherwise IE will display its own content.
public class MainController
{
public ActionResult Error()
{
//404 - Tell search engine not to display this page on search results
Response.StatusCode = 404;
Response.StatusDescription = "Not Found";
return View();
}
}
Register route that eats anything
Open Global.asax(.cs) in the MVC project and register error page route. Rule should be designed to capture all requests that do not fit in previous route rules. Simple solution: {*anything} rule captures any path including slashes, e.g. /shop/mushrooms-123.
//Somewhere in Global.asax
public static void RegisterRoutes(RouteCollection routes)
{
//Registered routes
//...
//Error route should be registered last
routes.MapRoute
(
"ErrorPage",
"{*anything}",
new { controller = "Main", action = "Error" }
);
}
Advantage: no redirects, requested URL string should be kept in browser’s location bar. For example, when
user requests /shop/mushrooms-123 in browser the error page content will be display but the URL will remain original.
The opposite of redirecting and displaying /error in location bar.
How to handle other errors?
Other errors in ASP .NET can be handled in Application_Error method in Global.asax.
//Somewhere in Global.asax
protected void Application_Error()
{
//Useful for debugging to get error details
Exception ex = Server.GetLastError();
//Redirect to error page
//Can be "/Main/Error" or "/error" or other route as "ErrorPage" rule eats all
Response.Redirect(Request.ApplicationPath + "/error/500");
}
Configuration in IIS 7.5
1. Open IIS Manager. Shortcut: click Start in Windows, type “inetmgr” in search box then press Enter.
2. Click on a website on the left side.
3. Select Error Pages on the right side.
4. Configure scenarios for specific status codes, as displayed on screenshots.


Troubleshooting
Q: Browser displays system error page when I expect custom error page.
A: Configure Error Pages for the website in IIS. Error Pages may differ between websites.
Q: Error handling is defined in web.config but does not work in Windows Server 2008.
A: Try to edit Error Pages directly in IIS (IIS Manager -> Sites -> (website name) -> Error Pages)
Q: Status code 200 OK displays a custom error page but 404 Not Found displays generic IIS error page (yellow box with techical message)
A: Configure Error Pages (see above). “customErrors” in web.config may not work as expected.
Q: Can I keep status 200 OK to display error page?
A: It will do the job for users who visually see and understand the content, but may not work for bots (i.e. displaying error page in search result)