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