Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get Action URL inside MVC4 .NET class

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.

like image 291
Thomas Avatar asked Jun 13 '13 17:06

Thomas


1 Answers

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");
like image 130
Ant P Avatar answered Nov 15 '22 23:11

Ant P