简体   繁体   中英

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. I have two options here:

Using 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.

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:

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

whereas <a href="@Url.Action( .... has following link location:

http://localhost:13974/AC/Logout

Both links are working fine when area attribute is removed but @Html.ActionLink() breaks when I specify the area

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)

For more info visit LinkExtensions.ActionLink

Additionally,

There is no overload as LinkExtensions.ActionLink Method (HtmlHelper, String, String, String, Object)

What you want is this overload:

//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. Change it to

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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