gpt4 book ai didi

c# - Asp.Net MVC : How do I get Html. ActionLink 正确呈现整数值?

转载 作者:太空狗 更新时间:2023-10-29 23:12:07 28 4
gpt4 key购买 nike

我有一个 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/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com