Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

From a page I have the following:

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

so controller is called correctly:

public ActionResult AddEntry(string ip, int TypeId, string returnUrl)
{
    // Do some stuff

    return Redirect(returnUrl);
}

My problem is that returnUrl gets null and it does not redirect to the same page that called the controller. Ideas?

Using: ASP.NET MVC 4 Razor

like image 728
Ralph Avatar asked Sep 18 '13 16:09

Ralph


People also ask

How redirect a specific view from controller in MVC?

You can use the RedirectToAction() method, then the action you redirect to can return a View. The easiest way to do this is: return RedirectToAction("Index", model); Then in your Index method, return the view you want.

Can TempData persist across action redirect?

TempData is used to pass data from current request to subsequent request (means redirecting from one page to another). It's life is very short and lies only till the target view is fully loaded. But you can persist data in TempData by calling Keep() method.

What is the difference between redirect and RedirectToAction in MVC?

RedirectToAction is meant for doing 302 redirects within your application and gives you an easier way to work with your route table. Redirect is meant for doing 302 redirects to everything else, specifically external URLs, but you can still redirect within your application, you just have to construct the URLs yourself.


4 Answers

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);
like image 80
vidriduch Avatar answered Sep 18 '22 07:09

vidriduch


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.

like image 30
Fals Avatar answered Sep 20 '22 07:09

Fals


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.

like image 28
Felipe Oriani Avatar answered Sep 20 '22 07:09

Felipe Oriani


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());
    }
like image 43
Esteban Elverdin Avatar answered Sep 20 '22 07:09

Esteban Elverdin