Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET MVC Routing: How do I redirect all actions to Base Url?

Is there anyway to make all actions in a given controller to redirect to the server root?

For example, if I have a URL with controller Home and action terms I want that to the URL to become /terms

If I have another URL with controller Home and action privacy, then the URL should become /privacy.

I am able to do this by hard-coding 2 routes, but is there a way to automatically do this?

routes.MapRoute(
    "Term",
    "terms",
    new { controller = "Home", action = "terms" }
 );

routes.MapRoute(
    "Privacy",
    "privacy",
    new { controller = "Home", action = "privacy" }
);
like image 933
Abe Avatar asked Jan 28 '26 13:01

Abe


1 Answers

routes.MapRoute(
    "ActionOnly",
    "{action}",
    new { controller = "Home" }
);
like image 168
Darin Dimitrov Avatar answered Feb 01 '26 16:02

Darin Dimitrov



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!