同一站点的 mvc 多个路由

mvc multiples routes for the same site

这是我的问题我有一个现有的网站

wwww.mysite.com

要求让我允许用户像这样使用相同的网站

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

所以我修改路由配置(RouteConfig.cs)

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

我为除 www.mysite.com/Home/Index

之外的所有预期路线工作

Div 所说的属性路由可能是更好的方法,但您可以在 中的 OrganisationRoute 之前添加固定路由RegisterRoutes() 方法如下:

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

您可以为此使用属性路由:

启用属性路由:
要启用属性路由,我们需要在RouteConfig中配置时调用路由集合class的MapMvcAttributeRoutes方法。

routes.MapMvcAttributeRoutes(); 

关于你的行动:

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