Search code examples
c#asp.netasp.net-mvcasp.net-mvc-routing

mvc multiples routes for the same site


here is my problem I have an existing web site

wwww.mysite.com

requirement ask me to allow users to use the same site like this

www.mysite.com
www.mysite.com/Home/Index
www.mysite.com/defaultsuborganisation/Home/Index
www.mysite.com/suborganisation1/Home/Index
www.mysite.com/suborganisation2/Home/Index

so I modify the routes configuration (RouteConfig.cs)

routes.MapRoute(
  name: "OrganisationRoute",
  url: "{org}/{controller}/{action}/{data}",
  defaults:
  new
    {
    org= "defaultsuborganisation",
    controller = "Home",
    action = "Index",
    data = UrlParameter.Optional
  });

I work for all the expected routes except for www.mysite.com/Home/Index


Solution

  • Attribute routing like Div said may be the better way to go, but you can add a fixed route ahead of your OrganisationRoute in the RegisterRoutes() method like so:

    routes.MapRoute(
     "DefaultHomeRoute",
     "Home/Index/{id}", 
     new { controller = "Home", action = "Index", id = UrlParameter.Optional}
     );