asp.net-mvcrouteshttp-redirect

How do I redirect to the previous action in ASP.NET MVC?


Lets suppose that I have some pages

that can call a common utility controller like

locale/change/es or authorization/login

How do I get these methods (change, login) to redirect to the previous actions (details, info, bar) while passing the previous parameters to them (5, bob, 7)?

In short: How do I redirect to the page that I just visited after performing an action in another controller?


Solution

  • try:

    public ActionResult MyNextAction()
    {
        return Redirect(Request.UrlReferrer.ToString());
    }
    

    alternatively, touching on what darin said, try this:

    public ActionResult MyFirstAction()
    {
        return RedirectToAction("MyNextAction",
            new { r = Request.Url.ToString() });
    }
    

    then:

    public ActionResult MyNextAction()
    {
        return Redirect(Request.QueryString["r"]);
    }