jmcd
jmcd

Reputation: 4300

How do I redirect a user to a custom 404 page in ASP.NET MVC instead of throwing an exception?

I want to be able to capture the exception that is thrown when a user requests a non-existent controller and re-direct it to a 404 page. How can I do this?

For example, the user requests http://www.nosite.com/paeges/1 (should be /pages/). How do I make it so they get re-directed to the 404 rather than the exception screen?

Upvotes: 18

Views: 15454

Answers (3)

jmcd
jmcd

Reputation: 4300

Found this on the same site - Strategies for Resource based 404s

Upvotes: 1

Dale Ragan
Dale Ragan

Reputation: 18270

Just use a route:

// We couldn't find a route to handle the request.  Show the 404 page.
routes.MapRoute("Error", "{*url}",
    new { controller = "Error", action = "404" }
);

Since this will be a global handler, put it all the way at the bottom under the Default route.

Upvotes: 16

Espo
Espo

Reputation: 41919

Take a look at this page for routing your 404-errors to a specified page.

Upvotes: 6

Related Questions