views:

140

answers:

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

+3  A: 

Use the RedirectToAction method on the controller:

return RedirectToAction( "action", "controller", new { id = redirectID } )
tvanfosson
Thanks. What if I just want the URL... but don't want to redirect the user?
Andrew Arnott
+2  A: 

What you are looking for is RedirectToAction.

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

Daniel A. White
Thanks, Daniel.
Andrew Arnott