Reputation: 10517
I'm having trouble doing what I want to achieve when doing routing in ASP.NET MVC. What I want to do is the following:
http://localhost/MyWebsite/
, I want to redirect to http://localhost/MyWebsite/Login/
(The redirection to the action Index
is implied here)http://localhost/MyWebsite/MyAction
, I want to redirect to http://localhost/MyWebsite/Login/MyAction
Point 1 was achieved using the following lines in the file RouteConfig.cs:
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Login", action = "Index", id = UrlParameter.Optional });
But I cannot accomplish point 2. Note that the user does not come from another action in a controller, but actually types the address in his browser.
Thanks for your help.
Upvotes: 1
Views: 276
Reputation: 1535
since /MyWebsite/Login
and /MyWebsite/MyAction
both have two segments in the URL, they're both matching your defined Route.
You can use a Route Constraint to only match /MyWebsite/Login
to your first point, followed by a modified second route mapping:
routes.MapRoute(
name: "Default",
url: "MyWebsite/Login/",
defaults: new { controller = "Login", action = "Index", id = UrlParameter.Optional });
routes.MapRoute(
name: "Actions",
url: "MyWebsite/{action}/",
defaults: new { controller = "Login", action = "Index", id = UrlParameter.Optional });
Upvotes: 2