7

Basically, I want the same functionality as Url.Action, but outside of the controller. Is this possible?

While I'm sure I'll use it elsewhere, my current desire is to generate action url's inside of a viewmodel.

tereško
  • 58,060
  • 25
  • 98
  • 150
Thomas
  • 3,664
  • 3
  • 15
  • 12
  • Create your own extension method just by appending strings together.. – Bhushan Firake Jun 13 '13 at 17:43
  • 1
    possible duplicate of [Call UrlHelper in models in ASP.NET MVC](http://stackoverflow.com/questions/2031995/call-urlhelper-in-models-in-asp-net-mvc) – Ant P Jun 13 '13 at 17:49

1 Answers1

20

You can instantiate a UrlHelper in your view model, like so:

UrlHelper helper = new UrlHelper(HttpContext.Current.Request.RequestContext);

Then, you can use this just like the one you use in your view:

string actionUrl = helper.Action("MyAction", "MyController");
Ant P
  • 24,820
  • 5
  • 68
  • 105
  • I [just read here](http://stackoverflow.com/a/11582207/1037948) that you want to avoid using `HttpContext.Current` -- not sure if that's applicable here, or even possible. – drzaus Sep 26 '13 at 19:55
  • @drzaus this is not ASP.NET Web API, so no, it's not applicable here. – Ant P Sep 26 '13 at 20:19