Abe
Abe

Reputation: 6516

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" }
);

Upvotes: 3

Views: 1811

Answers (2)

Darin Dimitrov
Darin Dimitrov

Reputation: 1038930

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

Upvotes: 6

Hogan
Hogan

Reputation: 70523

Hard coding the two routes is the automatic way.

Upvotes: 0

Related Questions