Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 804
jmcd Avatar asked Aug 21 '08 13:08

jmcd


People also ask

How do you make a catch all route to handle 404 Page Not Found queries for ASP NET MVC?

a) add that last route to your route list. b) create a controller (in my example, i called it StaticContentController) with an Action method (in my example, i added a method called PageNotFound(..)) add logic this method to display the 404 page not found, View. If you still have the default route (I.E.

How do I enable custom errors in web config MVC?

The first step is to enable customErrors using its mode attribute that can have one of the following three values: On: Specifies that custom errors are enabled. If no defaultRedirect is specified, users see a generic error page e.g. Error. cshtml in ASP.NET MVC application.


1 Answers

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.

like image 145
Dale Ragan Avatar answered Oct 02 '22 23:10

Dale Ragan