cycopepe
cycopepe

Reputation: 551

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

Upvotes: 2

Views: 56

Answers (2)

Divyang Desai
Divyang Desai

Reputation: 7866

You can use attribute routing for this:

Enabling Attribute Routing:
To enable Attribute Routing, we need to call the MapMvcAttributeRoutes method of the route collection class during configuration in RouteConfig.

routes.MapMvcAttributeRoutes(); 

And on your action:

[Route("Home/Index")]
public ActionResult Index()
{
  return view();
}

Upvotes: 0

Stinky Towel
Stinky Towel

Reputation: 778

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

Upvotes: 2

Related Questions