2013-04-17 36 views
3

我添加了一些类似这样的MVC框架:路由配置顺序路线

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

一个自定义路由现在即时得到一些问题,当我试图从一个控制器调用的方法,这是工作好之前添加新的路由

<a id="someId" class="link-button" href="../Documents/Create"><span>Create</span></a> 

现在我可以做到这一点的唯一方法是使用类似href="EN/us/Documents/Create"

是有保留的自定义路由为我的客户端,并仍保持012的方式我的管理员端的方式,这是因为我在管理端开发了几个功能,但现在我必须包括客户端的自定义路由。非常感谢。

有我的路线,如今

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

routes.MapRoute(
      name: "CustomRoute", 
      url: "{country}/{lang}/{controller}/{action}", 
      defaults: new { controller = "Test", action = "Index" } 
     ); 

但我只能访问CustomRoute与/ ES/ES /测试/索引...为什么不采取默认值?

回答

1

你只需要声明您的自定义路线后的默认路由:

routes.MapRoute(
    name: "Default", 
    url: "{controller}/{action}/{id}", 
    defaults: new { controller = "Home", action = "Index", 
     id = UrlParameter.Optional } 
); 
// SomeOther = someothercontroller 
routes.MapRoute(
    name: "CustomRoute", 
    url: "{coutry}/{lang}/{controller}/{action}", 
    defaults: new { controller = "SomeOther", action = "Index" } 
); 
+0

默认路由配置应始终在路由配置的末尾。由于路由配置将解析匹配的配置,它将接受并路由到该路径。因此,如果您在开始时指定默认配置,它将始终匹配并路由到该路径。 – Saanch

+0

我知道这是我的第一个答案,请参阅我的编辑,但仔细看看他的例子,情况并非如此。 –

+0

我真的很厌烦你的帮助,请检查我的编辑,并告诉我,如果你知道发生了什么... – Steve

1

您已经取代了默认RouteConfig到新的配置,它并在此{coutry}匹配URL/{ lang}/{controller}/{action}格式。

如果您想接受../Documents/Create Url您必须在最后添加默认的RouteConfig。

routes.MapRoute(
    name: "CustomRoute", 
    url: "{coutry}/{lang}/{controller}/{action}", 
    defaults: new { controller = "Documents", action = "Index" } 
); 

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

另外,在锚标记<a id="someId" class="link-button" href="../Documents/Create"><span>Create</span></a>,而不是硬编码href你可以写类似以下。

<a id="someId" class="link-button" href="@Url.Action("Create","Documents")><span>Create</span></a>

+0

谢谢你的帮助,它的stil不适合我,请查看我的编辑并告诉我,如果你知道是什么正在发生? – Steve

+0

@Steve:更新我的答案,请尝试。 – Saanch

+0

你是对的,我试了你的答案,非常感谢你,但现在即时通讯问题,我只能访问CustomRoute与/ ES/ES /测试/索引,它是主页,我需要访问它/只有一些想法? – Steve