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 this Url. Action("actionName", "controllerName", new { Area = "areaName" });
There is a difference. Html. ActionLink generates an <a href=".."></a> tag whereas Url. Action returns only an url.
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.
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.
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With