Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 617
David MZ Avatar asked Aug 14 '11 14:08

David MZ


1 Answers

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.

like image 100
Ofer Zelig Avatar answered Sep 20 '22 07:09

Ofer Zelig