在Asp.net MVC 4中指定area属性时,ActionLink和Url.Action将定位不同的位置

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

使用ActionLink

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

要么

 Logout 

我正在指定区域因为我想调用AC控制器的动作方法而不管它所在的区域。

据我所知,@ Html.ActionLink()和@ Url.action之间的区别是首先生成一个锚标记,其中第二个返回一个url(如果我错了请纠正我),所以我猜两者都应该针对相同位置,但在这里@ Html.ActionLink有以下链接位置:

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

<a href="https://stackoverflow.com/questions/20739206/actionlink-and-url-action-target-different-locations-when-area-attribute-is-spec/@Url.Action( ….有以下链接位置:

 http://localhost:13974/AC/Logout 

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

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

您可以使用

 @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)没有重载

你想要的是这个重载:

// linkText,actionName,controllerName,routeValues,htmlAttributes

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

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

您正在使用ActionLink方法的错误重载。 将其更改为

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