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?
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With