Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make a catch all route to handle '404 page not found' queries for ASP.NET MVC?

Is it possible to create a final route that catches all .. and bounces the user to a 404 view in ASP.NET MVC?

NOTE: I don't want to set this up in my IIS settings.

like image 848
Pure.Krome Avatar asked Nov 21 '08 23:11

Pure.Krome


People also ask

How set multiple route in MVC?

Multiple Routes You need to provide at least two parameters in MapRoute, route name, and URL pattern. The Defaults parameter is optional. You can register multiple custom routes with different names. Consider the following example where we register "Student" route.

How can use route in ASP.NET MVC?

The ASP.NET Routing module is responsible for mapping incoming browser requests to particular MVC controller actions. By the end of this tutorial, you will understand how the standard route table maps requests to controller actions.

What is RouteConfig in asp net?

In MVC, routing is a process of mapping the browser request to the controller action and return response back. Each MVC application has default routing for the default HomeController. We can set custom routing for newly created controller. The RouteConfig. cs file is used to set routing for the application.


1 Answers

Found the answer myself.

Richard Dingwall has an excellent post going through various strategies. I particularly like the FilterAttribute solution. I'm not a fan of throwing exceptions around willy nilly, so i'll see if i can improve on that :)

For the global.asax, just add this code as your last route to register:

routes.MapRoute(     "404-PageNotFound",     "{*url}",     new { controller = "StaticContent", action = "PageNotFound" }     ); 
like image 182
Pure.Krome Avatar answered Oct 09 '22 06:10

Pure.Krome