2

In a View, code like this will generate the right URL to jump to controller's action method based on the routes in your global.asax.cs file.

<%= Html.ActionLink("text", "action", "controller") %>

My question is how can I achieve a similar route-to-URL mapping outside a view, such as a Controller? There is no Html member on the Controller class on which to call ActionLink.

Some controller actions need to redirect the browser, and I want to redirect to a controller and action without hard-wiring the URL into the controller, which would break if I changed the way my routes mapped these URLs to controllers and actions

1
  • i updated my response to your question. Commented May 2, 2009 at 23:19

2 Answers 2

4

Use the RedirectToAction method on the controller:

return RedirectToAction( "action", "controller", new { id = redirectID } )
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks. What if I just want the URL... but don't want to redirect the user?
3

What you are looking for is RedirectToAction.

If you want just the url, use the Url property of the controller - its a UrlHelper.

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.