简体   繁体   English

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

[英]ActionLink and Url.Action target different locations when area attribute is specified in Asp.net MVC 4

In _LoggedInUser.cshtml (which is in Views/Shared folder at application's root) view I want to call the Logout method of AC controller. 在_LoggedInUser.cshtml(位于应用程序根目录下的Views / Shared文件夹中)视图中,我想调用AC控制器的Logout方法。 I have two options here: 我有两个选择:

Using ActionLink 使用ActionLink

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

OR 要么

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

I am specifying area because I want to call action method of AC controller irrespective of area it is in. 我指定区域因为我想调用AC控制器的动作方法而不管它在哪个区域。

As far as I understand the difference between @Html.ActionLink() and @Url.action is that first generates an anchor tag where as second returns a url (Please correct me if I am wrong), so I guess both should target to same location but here @Html.ActionLink has following link location: 据我所知,@ Html.ActionLink()和@ Url.action之间的区别是首先生成一个锚标记,其中第二个返回一个网址(如果我错了请纠正我),所以我猜两个都应该针对相同位置但在这里@ Html.ActionLink有以下链接位置:

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

whereas <a href="@Url.Action( .... has following link location: <a href="@Url.Action( ....有以下链接位置:

http://localhost:13974/AC/Logout

Both links are working fine when area attribute is removed but @Html.ActionLink() breaks when I specify the area 当删除区域属性时,两个链接都正常工作,但是当我指定区域时,@ Html.ActionLink()会中断

Why both the links are targeting to different locations when I specify area? 当我指定区域时,为什么两个链接都定位到不同的位置?

You can use 您可以使用

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

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

For more info visit LinkExtensions.ActionLink 有关更多信息,请访问LinkExtensions.ActionLink

Additionally, 另外,

There is no overload as LinkExtensions.ActionLink Method (HtmlHelper, String, String, String, Object) LinkExtensions.ActionLink Method (HtmlHelper, String, String, String, Object)没有重载LinkExtensions.ActionLink Method (HtmlHelper, String, String, String, Object)

What you want is this overload: 你想要的是这个重载:

//linkText, actionName, controllerName, routeValues, htmlAttributes // linkText,actionName,controllerName,routeValues,htmlAttributes

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

try and let us know if this resolves the issue. 如果这解决了问题,请尝试告诉我们。

You are using the wrong overload of the ActionLink method. 您正在使用ActionLink方法的错误重载。 Change it to 将其更改为

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM