Andrew Arnott
Andrew Arnott

Reputation: 81791

How to turn route into URL in ASP.NET MVC Controller?

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

Upvotes: 2

Views: 436

Answers (2)

Daniel A. White
Daniel A. White

Reputation: 190945

What you are looking for is RedirectToAction.

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

Upvotes: 3

tvanfosson
tvanfosson

Reputation: 532465

Use the RedirectToAction method on the controller:

return RedirectToAction( "action", "controller", new { id = redirectID } )

Upvotes: 4

Related Questions