views:

490

answers:

2

I have a controller named "AccountController" and action called "ResetPassword". From the html markup I can do something like:

<%= Html.ActionLink("LinkText", "ResetPassword", "AccountController") %>

and it will output the correct url. Is there a way to do this programmatically? I am trying to get the url as a string in another class but System.Web.Mvc.HtmlHelper doesn't have an ActionLink() static method and creating an instance of the class doesn't have it either.

Note: If I try to create instances of HtmlHelper or UrlHelper I then need references to the View Context or Request Context and I can't figure out how to get those from the controller (to pass in to my class method)

+1  A: 

Use Url.Action instead.

Daniel A. White
When I try to create an instance of UrlHelper it asks for a "requestContext". How do I get the requestContext so I can create an UrlHelper instance?
codette
If you are in a controller, use the HttpContext property of the controller.
Daniel A. White
I am in a class outside the controller and HttpContext.Current will not work. I get a syntax error, it's the wrong type.
codette
Try passing in the ViewContext then. Sorry.
Daniel A. White
Since I am calling my method from the Controller there doesn't seem to be any ViewContext
codette
Can't you call it in your controller and pass the result to the other class/function?
Daniel A. White
A: 

Check out Url Form Action Without ViewContext .

J.W.