Massoud Mazar

Sharing The Knowledge

NAVIGATION - SEARCH

Quick and easy Custom 404 page for ASP.net MVC 5

While ASP.net MVC makes it easy and clean to create a professional looking website, it seems to be a little harder to incorporate custome error pages (e.g. 404 page) which are part of general look and feel of your site. There are many good write-ups on how to do this (for example http://benfoster.io/blog/aspnet-mvc-custom-error-pages), but I was looking for an easy way to point to one of my views as the 404 page.

What I ended up doing was to create a controller for my error views and use the httpErrors section of web.config to point to it:


    
      
      
    

As you can guess, "/Error/NotFound" points to my Controller and View for the 404 error. Controller code looks like this:

public class ErrorController : Controller
{
    public ActionResult NotFound()
    {
        Response.StatusCode = 404;
        return View();
    }
}

Comments (1) -

For anyone thinking of trying this approach remember that Web.config syntax is camelCase

Reply

Add comment