Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best practice on forming links to MVC /controller/actions

I was wondering what the best practice was for specifying an action on a certain controller.

Parts of the code I've been working on are specifying a url as:

<a href="/controller/action"/>

I'm generally not a big fan of this style. I've preferred to use:

<a href='@Url.Action("Action", "Controller")'/>

1) What is the best practice in forming urls for internal actions in this case? Both work, just wondering what is better.

2) Is there a benefit to using one versus the other?

like image 570
Alexander Matusiak Avatar asked Jan 10 '23 21:01

Alexander Matusiak


1 Answers

Of the two options I would say the second is the better approach as it will work with virtual paths/directories:

<a href='@Url.Action("Action", "Controller")'/>

I prefer to use ActionLinks though:

@Html.ActionLink("text", "action", "controller")
like image 78
hutchonoid Avatar answered Jan 17 '23 16:01

hutchonoid