ASP.NET MVC 4 - Redirect to the same page after controller ends

You could use a Request.QueryString method to get some values from URL, for sample:

@using (Html.BeginForm("AddEntry", "Configure", FormMethod.Get, null))
{
    @Html.TextBox("ip")
    @Html.Hidden("TypeId", 1)
    @Html.Hidden("returnUrl", this.Request.RawUrl)
    <input type="submit" value="@Resource.ButtonTitleAddComponent" />
}

And in your controller, receive it as a parameter string returnUrl.


You can get the Refer URL from the Request in the controller:

public ActionResult AddEntry(string ip, int TypeId, string returnUrl)
{

     // Do some stuff
     string url = this.Request.UrlReferrer.AbsolutePath;

     return Redirect(url);
}

This will redirect you exactly to the calling URL.


in your controller class use Request.UrlReferrer. There's no need to pass the url from the page.

   public ActionResult AddEntry(string ip, int TypeId)
    {

         // Do some stuff

         return Redirect(Request.UrlReferrer.ToString());
    }

you can also do this if you need to return to something like details page and return to the same page with a query:

return Redirect(Request.UrlReferrer.PathAndQuery);