DISABLE ADBLOCK

ADBlock is blocking some content on the site

ADBlock errore

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

StackOverflow https://stackoverflow.com/questions/815845
 Checked

Question

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

Solution

What you are looking for is RedirectToAction.

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

OTHER TIPS

Use the RedirectToAction method on the controller:

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow