6

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)

2 Answers 2

10

Use Url.Action instead.

Sign up to request clarification or add additional context in comments.

6 Comments

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?
If you are in a controller, use the HttpContext property of the controller.
I am in a class outside the controller and HttpContext.Current will not work. I get a syntax error, it's the wrong type.
Try passing in the ViewContext then. Sorry.
Since I am calling my method from the Controller there doesn't seem to be any ViewContext
|
1

Check out Url Form Action Without ViewContext .

Comments

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.