2

I have a requirement in which I have to map the below url

/amer/us/en/ = Home controller
/amer/us/en/login/index = Home controller
/amer/us/en/confirmation = Confirmation controller

along with the regular default action.

Eg if user goes to

http:\\test.com --> http://test/home/index
 http:\\test.com/amer/us/en/login/index  --> http://test/home/index
 http:\\test.com/amer/us/en/   --> http://test/home/index

I was looking into attribute routing and so I added the below code in HomeController

  [RoutePrefix("amer/us/en/")]
    [Route("{action=index}")]
    public class HomeController : Controller
    {

    }

and I am getting this error The route prefix 'amer/us/en/' on the controller named 'Home' cannot begin or end with a forward slash and also the default routing is not working now so http://test.com is not loading anything. Below is my default RouteConfig class.

 public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            routes.MapMvcAttributeRoutes();

            routes.MapRoute(
                name: "Default",
                url: "{controller}/{action}/{id}",
                defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
            );
        }

Very new to MVC. Can someone tell me what I am doing wrong here.

1 Answer 1

5

Routing in MVC works either by defining your routes in the RouteConfig class or by attribute routing (or you can use Areas). Routing with RouteConfig works with the order you define the routes with. When a request comes, MVC will try your routes from top to bottom and execute the first one that it can match with the requested url. So the routing need in your example can be achieved by:

routes.MapRoute(
            name: "RootLogin",
            url: "amer/us/en/login/index/{id}",
            defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
        );

routes.MapRoute(
            name: "DefaultAmer",
            url: "amer/us/en/{controller}/{action}{id}",
            defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
        ); 

routes.MapRoute(
                name: "Default",
                url: "{controller}/{action}/{id}",
                defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
            );

this will map login as a special route and all the other /amer/us/en/ routes will go to whatever controller comes after and whatever action of it. The last route, if the request does not start with /amer/us/en will perform the default behavior.

Looks like, however, you want to define /amer/us/en/ as an Area, so you might want to take a look into that as well.

Sign up to request clarification or add additional context in comments.

1 Comment

Works great..Thank u so much.

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.