2013-03-04 64 views
0

我遇到了MVC4中的路由问题。路径段参数问题

我有一些生活在特定产品之外的行为,还有更多生活在用户选择的产品中的行为。为了适应动作我已经绘制两条路线

 context.MapRoute(
      "CMS_product", 
      "CMS/{productId}/{controller}/{action}/{id}", 
      new { controller = MVC.CMS.Home.Name, action = MVC.CMS.Home.ActionNames.Index, productId = default(Guid).ToString(), id = UrlParameter.Optional }, 
      new string[] { "Areas.CMS.Controllers" } 
     ); 

     context.MapRoute(
      "CMS_default", 
      "CMS/{controller}/{action}/{id}", 
      new { controller = MVC.CMS.Home.Name, action = MVC.CMS.Home.ActionNames.Index, id = UrlParameter.Optional }, 
      new string[] { "Areas.CMS.Controllers" } 
     ); 

因此,尽管这部作品在一个通用的,因为,没有我的路线将任何匹配缺省路由更长,而不是让一个URL像

〜/CMS /产品/列表

在产品外部操作时,我会得到像这样的网址。

〜/ CMS/00000000-0000-0000-0000-000000000000 /产品/列表

另注:我曾尝试硬编码Prodcut /列表中的路线,并在放置它CMS_product前它希望在其他网址之前匹配。我觉得我必须忽略简单的东西。

回答

1

为了完整起见,如果其他人遇到类似的问题,这里是解决方案。

 // used to match ~/CMS/00000000-0000-0000-0000-000000000000/Product/List 
     // prevents the GUID.Empty from showing when there is no product value 
     // in the segment 
     context.MapRoute(
      name: "CMS_nullproduct", 
      url: "CMS/{controller}/{action}/{id}", 
      defaults: new { controller = MVC.CMS.Home.Name, action = MVC.CMS.Home.ActionNames.Index, id = UrlParameter.Optional }, 
      constraints: new { productId = Guid.Empty.ToString() }, 
      namespaces: new string[] { "Areas.CMS.Controllers" } 
     ); 

     // matches any route with a productId segment value of anything aside from 
     // GUID.Empty 
     context.MapRoute(
      name: "CMS_product", 
      url: "CMS/{productId}/{controller}/{action}/{id}", 
      defaults: new { controller = MVC.CMS.Home.Name, action = MVC.CMS.Home.ActionNames.Index, id = UrlParameter.Optional }, 
      constraints: new { productId = @"^(\{{0,1}([0-9a-fA-F]){8}-([0-9a-fA-F]){4}-([0-9a-fA-F]){4}-([0-9a-fA-F]){4}-([0-9a-fA-F]){12}\}{0,1})$" }, 
      namespaces: new string[] { "Areas.CMS.Controllers" } 
     ); 

     context.MapRoute(
      name: "CMS_default", 
      url: "CMS/{controller}/{action}/{id}", 
      defaults: new { controller = MVC.CMS.Home.Name, action = MVC.CMS.Home.ActionNames.Index, id = UrlParameter.Optional }, 
      namespaces: new string[] { "Areas.CMS.Controllers" } 
     ); 
+0

谢谢!我无法弄清楚如何从我的网址中删除'?pID = 00000000-0000-0000-0000-000000000000'。这个伎俩。 – 2013-08-05 18:30:36

+0

Np,很高兴帮助 – Siegeon 2013-08-05 20:03:13

0

在我看来,你应该删除productId的默认值。

context.MapRoute(
     "CMS_product", 
     "CMS/{productId}/{controller}/{action}/{id}", 
     new { controller = MVC.CMS.Home.Name, action = MVC.CMS.Home.ActionNames.Index, id = UrlParameter.Optional }, 
     new string[] { "Areas.CMS.Controllers" } 
    ); 

如果你不能提供的productId您路由引擎应该匹配第二条路线,并生成〜/ CMS /产品/目录,但如果你提供的productId它匹配第一个规则。

另外,您可以编写自定义IRouteConstraint或使用正则表达式来限制productId值。

context.MapRoute(
     "CMS_product", 
     "CMS/{productId}/{controller}/{action}/{id}", 
     new { controller = MVC.CMS.Home.Name, action = MVC.CMS.Home.ActionNames.Index, id = UrlParameter.Optional }, 
     new { productId = @"^\d{8}\-\d{4}\-\d{4}\-\d{4}\-\d{12}$" } 
     new string[] { "Areas.CMS.Controllers" } 
    ); 
+0

看起来,如果没有productId段的默认值,那么它不会得到维护。换句话说,它是第一个链接,但之后的任何操作都不会保留段值。 – Siegeon 2013-03-04 21:34:19