简体   繁体   English

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

[英]Asp.Net MVC: How do I get Html.ActionLink to render integer values properly?

I have an asp.net mvc application with a route similar to: 我有一个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}" }
);

My controller action method signature looks like: 我的控制器操作方法签名如下所示:

public ActionResult Detail(int year, int month, int day, string friendlyName)
{ // Implementation... }

In my view, I'm doing something like: 在我看来,我正在做类似的事情:

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

While the url that is generated with ActionLink works, it uses query string variables rather than URL rewriting. 使用ActionLink生成的url可以使用时,它使用查询字符串变量而不是URL重写。

For example, it would produce /blog/detail/my-slug?year=2008&month=7&day=5 instead of /blog/detail/2008/07/05/my-slug 例如,它将生成/ blog / detail / my-slug?year = 2008&month = 7&day = 5而不是/ blog / detail / 2008/07/05 / my-slug

Is there a way to get the generic version of ActionLink to properly pad the integer values so that the url comes out as expected? 是否有一种方法可以使ActionLink的通用版本正确填充整数值,以便按预期方式显示url?

Thanks 谢谢

Jim 吉姆

The fact that your parameters are integers has nothing to do with your problem. 您的参数是整数这一事实与您的问题无关。 The route definition you want to be used isn't actually being used, which is why the generated URL is using query string parameters instead of building the structure you want. 实际上并没有使用要使用的路由定义,这就是为什么生成的URL使用查询字符串参数而不是构建所需的结构的原因。

Routes are evaluated top-down, so you likely have a more generic route definition that is satisfying your requested URL generation. 路由是自上而下评估的,因此您可能拥有更通用的路由定义,可以满足您请求的URL生成。 Try moving the route you displayed in this post to the top of your route definitions, and you'll see that your generated link is as you'd expect. 尝试将您在此文章中显示的路线移动到路线定义的顶部,您会看到生成的链接符合您的期望。 Then look into modifying your route definitions to either be more specific, or just move them around as necessary. 然后考虑修改您的路线定义以使其更加具体,或者仅在必要时移动它们。

Debugging these types of scenarios can be a huge pain. 调试这些类型的场景可能会非常痛苦。 I'd suggest downloading Phil Haack's route debugger , it will make your life a lot easier. 我建议下载Phil Haack的路由调试器 ,它将使您的生活更加轻松。

I would suggest formatting the Year, Month, and Day as Strings instead. 我建议将Year,Month和Day格式化为字符串。 Think about this: Will you be doing any math on these "integers"? 考虑一下:您是否会对这些“整数”进行数学运算? Probably not, so there really is no point for making them integers. 可能不是,因此使它们成为整数确实没有意义。 Once you have them as Strings you can force the leading zero format. 将它们设置为字符串后,可以强制使用前导零格式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM