从 .net 3.5 升级到 4.0 后,MVC Html.ActionLink 不起作用

发布于 2024-10-07 21:06:19 字数 308 浏览 1 评论 0原文

正如标题所说,我有一个带有一堆 Html.ActionLink 的项目

<%= Html.ActionLink("Reason for booking", "BookingReason")%>

,我已将项目升级到 .net 4.0,并且它们不再呈现指向操作的链接。当然,它只是一个空白,导致页面重新加载。

Html.ActionLink 标记位于区域共享文件夹中的用户控件 (ascx) 中。

我尝试再次降级到 .net 3.5 并且它有效 - 奇怪。有什么想法吗?

谢谢

Well as the title says, I have a project with a bunch of Html.ActionLink

<%= Html.ActionLink("Reason for booking", "BookingReason")%>

I have upgraded the project to .net 4.0 and they no longer render a link to the action. Its just a blank of course causing the page to reload.

The Html.ActionLink tags are in a user control (ascx) in the Shared folder of an Area.

I tried downgrading again to .net 3.5 and it works - weird. any ideas?

Thx

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

苯莒 2024-10-14 21:06:19

请务必使用 UrlParameter.Optional 标记您的可选参数,

            routes.MapRoute(
            "Default", // Route name
            "{controller}/{action}/{id}", // URL with parameters
            new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
        );

这就是我们的问题(症状与您一样)。

——编辑
这不是唯一的问题。
当迁移到 MVC 3 时,你可能会遇到下一个问题,
假设您有这样的路线(idpage 是可选的),

            routes.MapRoute(
            "Default", // Route name
            "{controller}/{action}/{id}/{page}", // URL with parameters
            new { controller = "Home", action = "Index", id = "", page = "" } // Parameter defaults
        );

现在您必须将其拆分为 2 个不同的路线:

        routes.MapRoute(
            "Paged",                                                     // Route name
            "{controller}/{action}/{id}/{page}", 
            new { controller = "Home", action = "Index", page = UrlParameter.Optional } // Parameter defaults
        );

        routes.MapRoute(
            "Default",                                                     // Route name
            "{controller}/{action}/{id}", 
            new { controller = "Home", action = "Index", id = UrlParameter.Optional}//, page = UrlParameter.Optional }  // Parameter defaults
        );

Be sure to mark your optional params with UrlParameter.Optional

            routes.MapRoute(
            "Default", // Route name
            "{controller}/{action}/{id}", // URL with parameters
            new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
        );

That was the problem for us (symptoms as yours).

-- Edited
It wasn't the only problem.
When migrating to MVC 3 you can get next problem,
suppose you had such route(id and page were optional)

            routes.MapRoute(
            "Default", // Route name
            "{controller}/{action}/{id}/{page}", // URL with parameters
            new { controller = "Home", action = "Index", id = "", page = "" } // Parameter defaults
        );

now you have to split it to 2 different routes:

        routes.MapRoute(
            "Paged",                                                     // Route name
            "{controller}/{action}/{id}/{page}", 
            new { controller = "Home", action = "Index", page = UrlParameter.Optional } // Parameter defaults
        );

        routes.MapRoute(
            "Default",                                                     // Route name
            "{controller}/{action}/{id}", 
            new { controller = "Home", action = "Index", id = UrlParameter.Optional}//, page = UrlParameter.Optional }  // Parameter defaults
        );
柠檬色的秋千 2024-10-14 21:06:19

排序了。

我需要在自定义路由之上有一条默认路由......必须在某个时候将其删除。

仍然很奇怪它在 .net 3.5 而不是 4 中的工作原理。

context.MapRoute(
            "Name",
            "AreaName/{action}",
            new { controller = "defaultController", action = "defaultAction" }
        );

Sorted it.

I needed to have a default route above my custom ones...must have removed it at some point.

Still weird how it works in .net 3.5 and not 4.

context.MapRoute(
            "Name",
            "AreaName/{action}",
            new { controller = "defaultController", action = "defaultAction" }
        );
随心而道 2024-10-14 21:06:19

你的问题就是我的答案..:)

那么工作的解决方案 <%= Html.ActionLink("Reason for booking", "BookingReason")%>

  • 在 3.5 中它适用于<%= Html.ActionLink("预订原因", "BookingReason")%>
  • 在 4.0 中,它与 <%: Html.ActionLink("预订原因", "BookingReason")%>

差异:3.5 - "=" 和 4.0 - ":"

希望这也能解决您的问题..

Your question was the answer for me.. :)

Well the solution to work <%= Html.ActionLink("Reason for booking", "BookingReason")%> is

  • In 3.5 it works with <%= Html.ActionLink("Reason for booking", "BookingReason")%>
  • In 4.0 it works with <%: Html.ActionLink("Reason for booking", "BookingReason")%>

Difference : 3.5 - "=" and 4.0 - ":"

Hope that solves your problem too..

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文