Is there any difference between HTML.ActionLink
vs Url.Action
or they are just two ways of doing the same thing?
When should I prefer one over the other?
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.
Razor pages have handler-methods which are HTTP verbs. So to call a method from your page you need to put On followed by the http verb you want then your method name . And in your view pass the name to the asp-page-handler without the OnPost or OnGet prefix or Async suffix.
@Html. ActionLink("name", "Action", "Controler", new { id= sentId, Style = "color:White" }, null);
Yes, there is a difference. Html.ActionLink
generates an <a href=".."></a>
tag whereas Url.Action
returns only an url.
For example:
@Html.ActionLink("link text", "someaction", "somecontroller", new { id = "123" }, null)
generates:
<a href="/somecontroller/someaction/123">link text</a>
and Url.Action("someaction", "somecontroller", new { id = "123" })
generates:
/somecontroller/someaction/123
There is also Html.Action which executes a child controller action.
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