1

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:

  1. When typing http://localhost/MyWebsite/, I want to redirect to http://localhost/MyWebsite/Login/ (The redirection to the action Index is implied here)
  2. When typing 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.

0

1 Answer 1

2

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 });
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.