David MZ
David MZ

Reputation: 3718

MVC3 RouteUrl inside a ViewModel

I need to create a route URL based on parameters as a part of a JSON return values.

What is the equivalent of Url.RouteUrl but to be used inside the controller code, So I can return a string in my Json result that contains the routeurl .

I need this done outside of the controller class, in a separate class, can this be done at all?

Upvotes: 6

Views: 1204

Answers (1)

Ofer Zelig
Ofer Zelig

Reputation: 17480

You can still use Url.RouteUrl, but in a slightly different way.

Place a using System.Web.Mvc; at the top of your class (of course, you might need to Add Reference to System.Web.Mvc).

Then get the Url object by:

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

and access as usual: Url.RouteUrl.

Upvotes: 12

Related Questions