开发者

MVC ActionLink generating NON-Restul URL AFTER adding constraints

I have a custom route without constraints that generates a Restful URL with an ActionLink.

Route -

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

Generates -

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

From -

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

If I add constraints like so.

            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}" }

It generates -

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

Extra information: it is added before the custom route.

Any ideas? Cheers


I was working on the same issue while writing my blog.. In the end I realised that my Urls will have to use 1 digit month numbers.. change your route definition to this, and it will work:

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}" }

Or you can pass 2 digit strings as your day/month route values.. but you might miss this in some places and have dead links, so I'd recommend the route constraints fix..

If you DO find a workaround - drop me a mail pls ^_^


Artiom is essentially right. Since your ActionLink code specifies single digit integers in the route values, the single digit fails against your constraint. So, you can either change the constraint as Artiom suggests, or slightly modify the ActionLink code so the route values are "strings" (in double quotes):

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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜