Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HTML.ActionLink vs Url.Action in ASP.NET Razor

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?

like image 547
Pankaj Upadhyay Avatar asked Oct 10 '11 05:10

Pankaj Upadhyay


People also ask

What is the use of HTML ActionLink?

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.

How do I call razor page?

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.

How do you change the ActionLink color in HTML?

@Html. ActionLink("name", "Action", "Controler", new { id= sentId, Style = "color:White" }, null);


1 Answers

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.

like image 118
Darin Dimitrov Avatar answered Oct 05 '22 06:10

Darin Dimitrov