作者热门文章
- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我有一个 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}" }
);
我的 Controller 操作方法签名如下所示:
public ActionResult Detail(int year, int month, int day, string friendlyName)
{ // Implementation... }
在我看来,我正在做类似的事情:
<%= Html.ActionLink<BlogController>(item => item.Detail(blog.PostedOn.Year, blog.PostedOn.Month, blog.PostedOn.Day, blog.Slug), blog.Title) %>
虽然使用 ActionLink 生成的 url 有效,但它使用查询字符串变量而不是 URL 重写。
例如,它会生成/blog/detail/my-slug?year=2008&month=7&day=5 而不是/blog/detail/2008/07/05/my-slug
有没有办法让通用版本的 ActionLink 正确填充整数值,以便 url 按预期出现?
谢谢
吉姆
最佳答案
您的参数是整数这一事实与您的问题无关。您想要使用的路由定义实际上并没有被使用,这就是为什么生成的 URL 使用查询字符串参数而不是构建您想要的结构的原因。
路由是自上而下评估的,因此您可能有一个更通用的路由定义来满足您请求的 URL 生成。尝试将您在这篇文章中显示的路线移动到路线定义的顶部,您会看到生成的链接与您预期的一样。然后考虑修改您的路由定义,使其更具体,或者根据需要移动它们。
调试这些类型的场景可能会非常痛苦。我建议下载 Phil Haack 的 route debugger ,它会让你的生活更轻松。
关于c# - Asp.Net MVC : How do I get Html. ActionLink 正确呈现整数值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/146367/
我是一名优秀的程序员,十分优秀!