4

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?

hutchonoid
  • 32,982
  • 15
  • 99
  • 104
Alexander Matusiak
  • 1,748
  • 2
  • 18
  • 29
  • What if you rename your action or controller? You have to go to every page and refactor manually. – Tiago Almeida Apr 02 '14 at 19:22
  • http://bubblogging.wordpress.com/2011/12/29/mvc-routing-outgoing-urls/ – Ilya Sulimanov Apr 02 '14 at 19:27
  • @TiagoAlmeida: technically, that's a problem with either version above. – Chris Pratt Apr 02 '14 at 20:27
  • Depends on what you want to do. Each one has specific advantages and use cases. I like to use `@Html.ActionLink` but there are times where I have to use the other way. I don't think `@Html.ActionLink` lets you specify an `` tag if you wanted to use one. – L_7337 Apr 02 '14 at 20:32

3 Answers3

4

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")
hutchonoid
  • 32,982
  • 15
  • 99
  • 104
  • Url.Action or Html.ActionLink also respects an application's custom routes, which goes along well with your virtual paths. – Andy Apr 02 '14 at 19:27
3

You can also create StronlyTyped ActionLinks if you really wanted too.

@(Html.ActionLink<CustomersController>(x => x.Index(), "Customers"))
Community
  • 1
  • 1
Erik Philips
  • 53,428
  • 11
  • 128
  • 150
2

@hutchonoid sort of touched on this, but virtual paths are just one part of the benefit. While the default route pattern is controller/action/id?, it doesn't have to be that way. Especially if you're using attribute routing in MVC5+, the URL could be literally anything, and may not even include the controller or action name.

By handling all your links using Url.Action or Html.ActionLink, you abstract the view's knowledge of the URL, which is a very good thing. True separation of concerns, which is fundamental to MVC, requires pieces to be as "dumb" as possible, only knowing or caring about the things that it needs to.

If you do end up using attribute routing, I would go a step further and actually recommend naming all your routes, and then using Url.RouteUrl/Html.RouteLink instead of the action-centric versions. This provides even greater abstraction, as you could then have a completely different action or even controller handle that named route, without changing any of your views. You could also technically do this with standard routing, but naming routes with standard routing would require you to manually define each route, instead of relying on a default route or set of default routes.

Chris Pratt
  • 232,153
  • 36
  • 385
  • 444