O'Reilly logo

ASP.NET MVC 2 Cookbook by Richard Kimber, Andrew Siemer

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Creating a 404 page via routing

In this recipe, we will take a look at a way to easily handle that pesky Page cannot be found error. Specifically, we will handle the 404 error by providing a very loose route at the bottom of our route dictionary. This route will catch any request that doesn't match any of our routes. Then, we will take a look at a few other scenarios where a 404 might be encountered.

How to do it...

  1. Create a new MVC application.
  2. Then open up the Global.asax, so that we can add a very open route.
  3. Add this wildcard route to your route table.

    Global.asax:

    routes.MapRoute("NothingMatched", "{*url}", new { controller = "Error", action = "Http404"});
    
  4. Next, we need to create an Error controller in the Controllers directory.
  5. Then add a new ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required