tags:

views:

133

answers:

1

When I have access to UrlHelper I can convert an ActionResult to a string (i.e. the actual URL) like this: urlHelper.RouteUrl(actionResult.GetRouteValueDictionary());

How can I do that same from a static method where I don't have access to UrlHelper? Thanks.

+3  A: 

Just add a using statement for System.Web.Mvc, and create an instance of the UrlHelper class in your static method.

Robert Harvey
I was hoping there was something hidden somewhere that I could use without instantiating a UrlHelper...
pbz
The need to perform the instantiation is implied in your question. It's not a big class. I suppose you could lift the relevant code from the class using Reflector, but that would essentially amount to the same thing.
Robert Harvey
By instantiating the class, you benefit from using the class in its pristine, tested form, and you get access to all of the RouteUrl overloads.
Robert Harvey
OK... how do I instantiate a UrlHelper class, i.e. it expects a RequestContext object? How can I get a hold of that? Is it somewhere in HttpContext.Current?
pbz
What I'm working on is an HTML helper, so I have an instance to HtmlHelper, in this case I can write new UrlHelper(htmlHelper.ViewContext.RequestContext); It would be nice to know how to get a hold of this RequestContext without htmlHelper...
pbz
That's probably the best way to do it.
Robert Harvey