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.

2
  • Create your own extension method just by appending strings together.. Commented Jun 13, 2013 at 17:43
  • 1
    possible duplicate of Call UrlHelper in models in ASP.NET MVC Commented Jun 13, 2013 at 17:49

1 Answer 1

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");
Sign up to request clarification or add additional context in comments.

2 Comments

I just read here that you want to avoid using HttpContext.Current -- not sure if that's applicable here, or even possible.
@drzaus this is not ASP.NET Web API, so no, it's not applicable here.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.