Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# MVC ActionLink在添加约束后生成非Restul URL_C#_Asp.net Mvc_Model View Controller_Routing_Constraints - Fatal编程技术网

C# MVC ActionLink在添加约束后生成非Restul URL

C# MVC ActionLink在添加约束后生成非Restul URL,c#,asp.net-mvc,model-view-controller,routing,constraints,C#,Asp.net Mvc,Model View Controller,Routing,Constraints,我有一个没有约束的自定义路由,它生成一个带有ActionLink的Restful URL 路线- routes.MapRoute( "Blog", // Route name "Blog/{d}/{m}/{y}", // URL with parameters, new { controller = "Blog", action = "Retrieve" } 产生- http://localhost:2875/Blog/12/

我有一个没有约束的自定义路由,它生成一个带有ActionLink的Restful 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
http://localhost:2875/Blog/Retrieve?d=12&m=1&y=2010
从-

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

   http://localhost:2875/Blog/12/1/2010
http://localhost:2875/Blog/Retrieve?d=12&m=1&y=2010
额外信息:它添加在自定义管线之前

有什么想法吗?
干杯

我写博客的时候也在写同样的问题。。最后,我意识到我的URL必须使用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位字符串作为日/月路线值。。但是,在某些地方,您可能会错过这一点,并且有死链接,因此我建议使用路由约束修复


如果你确实找到了解决办法,请给我发封邮件,Artiom基本上是对的。由于ActionLink代码在路由值中指定了一位整数,因此该位整数无法满足您的约束。因此,您可以按照Artiom的建议更改约束,或者稍微修改ActionLink代码,使路由值为“字符串”(双引号):

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