Asp.Net MVC:如何让Html.ActionLink正确呈现整数值?

Jim*_*rts 5 c# asp.net-mvc routing

我有一个asp.net mvc应用程序,其路由类似于:

routes.MapRoute("Blog", 
    "{controller}/{action}/{year}/{month}/{day}/{friendlyName}",                          
    new { controller = "Blog", action = "Index", id = "", friendlyName="" }, 
    new { controller = @"[^\.]*", 
          year = @"\d{4}", 
          month = @"\d{2}", 
          day = @"\d{2}" }
);
Run Code Online (Sandbox Code Playgroud)

我的控制器操作方法签名如下所示:

public ActionResult Detail(int year, int month, int day, string friendlyName)
{ // Implementation... }
Run Code Online (Sandbox Code Playgroud)

在我看来,我做的事情如下:

<%= Html.ActionLink<BlogController>(item => item.Detail(blog.PostedOn.Year, blog.PostedOn.Month, blog.PostedOn.Day, blog.Slug), blog.Title) %>
Run Code Online (Sandbox Code Playgroud)

虽然使用ActionLink生成的url有效,但它使用查询字符串变量而不是URL重写.

例如,它会生成/ blog/detail/my-slug?year = 2008&month = 7&day = 5而不是/ blog/detail/2008/07/05/my-slug

有没有办法让ActionLink的通用版本正确填充整数值,以便url按预期出现?

谢谢

吉姆

Gil*_*gan 2

我建议将年、月和日格式化为字符串。想一想:您会对这些“整数”进行数学运算吗?可能不是,所以确实没有必要将它们设为整数。一旦将它们作为字符串,您就可以强制使用前导零格式。