2010-04-16 14 views
3

我有一個沒有約束的自定義路由,它會生成一個帶有ActionLink的Restful URL。MVC ActionLink在添加約束之後生成NON-Restul URL

路線 -

routes.MapRoute(
      "Blog", // Route name 
      "Blog/{d}/{m}/{y}", // URL with parameters, 
      new { controller = "Blog", action = "Retrieve" } 

生成 -

http://localhost:2875/Blog/12/1/2010 

從 -

<%=Html.ActionLink("Blog Entry - 12/01/2010", "Retrieve", "Blog", new { d = 12, m = 01, y = 2010 }, null)%> 

如果我添加的約束,像這樣。

  routes.MapRoute(
      "Blog", // Route name 
      "Blog/{d}/{m}/{y}", // URL with parameters, 
      new { controller = "Blog", action = "Retrieve" }, 
      new { d = @"\d{2}", m = @"\d{2}", y = @"\d{4}" } 

它產生 -

http://localhost:2875/Blog/Retrieve?d=12&m=1&y=2010 

額外的信息:它是在自定義路由前加入。

任何想法? 乾杯

回答

4

我就同一問題工作一邊寫我的博客。在我意識到,我的網址將不得不使用1位數字一個月..你的路由定義更改此結束,它會工作:

routes.MapRoute(
    "Blog", // Route name 
    "Blog/{d}/{m}/{y}", // URL with parameters, 
    new { controller = "Blog", action = "Retrieve" }, 
    new { d = @"\d{1,2}", m = @"\d{1,2}", y = @"\d{4}" } 

或者你也可以通過2位的字符串作爲你的天/月路由值..但你可能在某些地方錯過這個和有死鏈接,所以我建議路線約束脩復..

如果你找到解決方法 - 給我發一封郵件請致電^ _^

1

Artiom基本上是對的。由於您的ActionLink代碼在路線值中指定了單個數字整數,因此單個數字會違反您的約束條件。所以,你可以改變約束爲Artiom建議,或者稍微修改ActionLink的代碼,這樣的路線值是「字符串」(雙引號):

Html.ActionLink("Blog Entry - 12/01/2010", "Retrieve", "Blog", new { d = "12", m = "01", y = "2010" }, null)