Note that there are some explanatory texts on larger screens.

plurals
  1. POIn ASP.NET MVC, preserve URL when return RedirectToAction
    primarykey
    data
    text
    <p>I have an action method, and depending on what is passed to it, I want to redirect to another action in another controller. The action and controller names are determined at run time. </p> <p>If I return RedirectToAction(), it will force a redirect and change the URL in the browser. What I would like is something like TransferToAction() that can transfer processing of the current request to another action, without doing a redirect. I seem to remember a method behaving like this in earlier previews, but I can't seem to find it in the RC of ASP.NET MVC.</p> <p>Do you know how I would do this?</p> <p>UPDATE</p> <p>I added the following route:</p> <pre><code>routes.MapRoute( "PageRouter", "{site}/{*url}", new { controller = "PageRouter", action = "RoutePage", site = "", url = "" } ); </code></pre> <p>And the PageRouter controller action RoutePage:</p> <pre><code>public ActionResult RoutePage(string site, string url) { var controller = new HomeController {ControllerContext = ControllerContext}; controller.RouteData.Values["controller"] = "Home"; controller.RouteData.Values["action"] = "Index"; return controller.Index(site, url); } </code></pre> <p>I had to set the controller and action in RouteData for the Home Index view to be rendered. Otherwise, it would look for an Index view in PageRouterController.</p> <p>I still need to figure out how to create a controller and its action knowing only their names. e.g. I'd like to be able to just call something like this:</p> <pre><code>public ActionResult RoutePage(string site, string url) { return InvokeAction("Home", "Index"); } </code></pre> <p>What should go in InvokeAction() ? Do I need to pass it any context?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload