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

0 votes
asked May 2, 2009 by adolfojp

Lets suppose that I have some pages

  • some.web/articles/details/5
  • some.web/users/info/bob
  • some.web/foo/bar/7

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?

7 Answers

0 votes
answered Jan 2, 2009 by darin-dimitrov

Pass a returnUrl parameter (url encoded) to the change and login actions and inside redirect to this given returnUrl. Your login action might look something like this:

public ActionResult Login(string returnUrl) 
    // Do something...
    return Redirect(returnUrl);
0 votes
answered May 2, 2009 by nathan-ridley


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() });


public ActionResult MyNextAction()
    return Redirect(Request.QueryString["r"]);
0 votes
answered May 5, 2012 by rahatur

If you are not concerned with unit testing then you can simply write:

return Redirect(ControllerContext.HttpContext.Request.UrlReferrer.ToString());
0 votes
answered May 1, 2013 by juanpieterse

If you want to redirect from a button in the View you could use:

@Html.ActionLink("Back to previous page", null, null, null, new { href = Request.UrlReferrer})
0 votes
answered Jan 2, 2015 by stom

In Mvc using plain html in View Page with java script onclick

<input type="button" value="GO BACK" class="btn btn-primary" 
onclick="location.href='@Request.UrlReferrer'" />

This works great. hope helps someone.

@JuanPieterse has already answered using @Html.ActionLink so if possible someone can comment or answer using @Url.Action

0 votes
answered Jan 9, 2016 by paola-captanovska

You could return to the previous page by using ViewBag.ReturnUrl property.

0 votes
answered Sep 15, 2017 by matthew-hudson

To dynamically construct the returnUrl in any View, try this:

    var formCollection =
        new FormCollection
                new FormCollection(Request.Form),
                new FormCollection(Request.QueryString)

    var parameters = new RouteValueDictionary();

        .Select(k => new KeyValuePair<string, string>(k, formCollection[k])).ToList()
        .ForEach(p => parameters.Add(p.Key, p.Value));

<!-- Option #1 -->
@Html.ActionLink("Option #1", "Action", "Controller", parameters, null)

<!-- Option #2 -->
<a href="/Controller/Action/@object.ID?returnUrl=@Url.Action(ViewContext.RouteData.Values["action"].ToString(), ViewContext.RouteData.Values["controller"].ToString(), parameters)">Option #2</a>

<!-- Option #3 -->
<a href="@Url.Action("Action", "Controller", new { object.ID, returnUrl = Url.Action(ViewContext.RouteData.Values["action"].ToString(), ViewContext.RouteData.Values["controller"].ToString(), parameters) }, null)">Option #3</a>

This also works in Layout Pages, Partial Views and Html Helpers

Related: MVC3 Dynamic Return URL (Same but from within any Controller/Action)

Welcome to Q&A, where you can ask questions and receive answers from other members of the community.
Website Online Counter