Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 226
shrekDeep Avatar asked Dec 23 '13 07:12

shrekDeep


People also ask

How can we use area in URL action?

You can use this Url. Action("actionName", "controllerName", new { Area = "areaName" });

What is difference between HTML ActionLink and URL action?

There is a difference. Html. ActionLink generates an <a href=".."></a> tag whereas Url. Action returns only an url.

What is ActionLink in MVC?

Html. ActionLink creates a hyperlink on a view page and the user clicks it to navigate to a new URL. It does not link to a view directly, rather it links to a controller's action.

What is attribute routing in ASP net Core?

Summary. Attribute routing in ASP.NET Core 3.0 allows us to define specific, customized routes for actions in our systems. Said routes are applied directly at the controller action level, allowing for high cohesion between route and action.


1 Answers

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)

like image 83
Satpal Avatar answered Sep 18 '22 13:09

Satpal