ASP.NET Route All URLs to a Single Action
2015/10/312 min read
bookmark this
Photo by Mitchell Luo on Unsplash
Table of Contents
Introduction
This post shows how to set up ASP.NET MVC routing so that all URLs are directed to a single controller action.
Catch-All Route
This will make the URL always go to the Home Controller's Index action.
routes.MapRoute(
"AllUrl",
"{*url}",
new { controller = "Home", action = "Index" }
);
Defining Specific Routes Before the Catch-All
This route will catch all URLs, but if you have any other URL you need to define, place it before this one. For example, if you want to define a test URL so that when the URL hits /test, it will go to the Home controller's Test action:
routes.MapRoute(
"TestRouteName",
"test",
new { controller = "Home", action = "test" }
);
routes.MapRoute(
"AllUrl",
"{*url}",
new { controller = "Home", action = "Index" }
);
Conclusion
Using a catch-all route with {*url} in ASP.NET MVC allows you to direct all URLs to a single action. Just remember to define any specific routes before the catch-all route, since routes are evaluated in order and the first match wins.