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

Viewed 25356

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?

3 Answers
Related