【问题标题】:ActionLink and Url.Action target different locations when area attribute is specified in Asp.net MVC 4当在 Asp.net MVC 4 中指定 area 属性时,ActionLink 和 Url.Action 定位不同的位置
【发布时间】:2014-01-11 09:59:18
【问题描述】:

在 _LoggedInUser.cshtml(位于应用程序根目录的 Views/Shared 文件夹中)视图中,我想调用 AC 控制器的 Logout 方法。我在这里有两个选择:

使用 ActionLink

@Html.ActionLink("Logout", "Logout", "AC", new { area = string.Empty })

<a href="@Url.Action("Logout", "AC", new { area = string.Empty })">Logout</a>

我指定区域是因为我想调用 AC 控制器的操作方法,而不管它在哪个区域。

据我了解@Html.ActionLink() 和@Url.action 之间的区别在于,首先生成一个锚标记,然后第二个返回一个url(如果我错了请纠正我),所以我想两者都应该定位到同一位置,但此处@Html.ActionLink 具有以下链接位置:

http://localhost:13974/Home/logout?Length=2

&lt;a href="@Url.Action(.... 有以下链接位置:

http://localhost:13974/AC/Logout

删除 area 属性后两个链接都可以正常工作,但当我指定区域时 @Html.ActionLink() 会中断

当我指定区域时,为什么两个链接都指向不同的位置?

【问题讨论】:

    标签: c# asp.net asp.net-mvc


    【解决方案1】:

    你可以使用

    @Html.ActionLink("Logout", "Logout", "AC", new { area = string.Empty }, null)
    

    你可以使用重载,LinkExtensions.ActionLink Method (HtmlHelper, String, String, String, Object, Object)

    欲了解更多信息,请访问LinkExtensions.ActionLink

    另外,

    LinkExtensions.ActionLink Method (HtmlHelper, String, String, String, Object)没有过载

    【讨论】:

      【解决方案2】:

      你想要的是这个重载:

      //linkText、actionName、controllerName、routeValues、htmlAttributes

      <%=Html.ActionLink("Logout", "Logout", "AC", new {area = string.Empty}, null) %>
      

      尝试让我们知道这是否能解决问题。

      【讨论】:

        【解决方案3】:

        您使用了错误的 ActionLink 方法重载。改成

        @Html.ActionLink("Logout", "Logout", "AC", null, new { area = string.Empty })
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多