2013-12-17 90 views
2

我希望用户能够使用“/ Linecard”或“/ Manufacturers”作为URL访问我的ASP.Net MVC网站的“/ Linecard”页面...所以同一控制器,2个不同的可能的URL。MVC:多路由1控制器

我尝试添加以下内容:

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

添加这种“默认”路线后都不能正常工作,我得到一个404错误,当我去“/制造商”。把它放在“Default”之前的作品中,但是当我点击菜单链接时,只有“/ Manufacturers”出现在URL中,因为它是第一个匹配。我希望“/ Linecard”始终显示为网址。

任何指针?我可以用来完成这个限制吗?谢谢!

回答

4

当我们移动到无扩展名的URL时,我遇到了同样的问题。我们需要继续支持带有扩展名的一条路线。我周围有通过让我默认路由适用于一切,除了旧的URL,然后后映射一个专为例外

routes.MapRoute(
    "Default", // Route name 
    "{controller}/{action}/{id}", // URL with parameters 
    new { controller = "Home", action = "Index", id = UrlParameter.Optional }, 
    // if controller specified does not match 'manufacturers' (case insensitive) 
    new { controller = "^((?i)(?!manufacturers).)*$" }, 
    new string[] { "Namespace.Of.Controllers" } 
); 

routes.MapRoute(
    "Manufacturers", // Route name 
    "Manufacturers/{action}/{id}", // URL with parameters 
    new { controller = "Linecard", action = "Index", id = UrlParameter.Optional }, 
    new string[] { "Namespace.Of.Controllers" } 
); 
+0

非常好......那正是我想要的。谢谢! –

0

末像映射与默认的路由时,您还可以设置一个订单所以

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

routes.MapRoute(
    "Default", // Route name 
    "{controller}/{action}/{id}", // URL with parameters 
    new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults 
);