Willy
Willy

Reputation: 10650

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

Upvotes: 21

Views: 80494

Answers (7)

Luke Alderton
Luke Alderton

Reputation: 3296

I found that using UrlReferrer works well and allows me to add on extra params if needed.

Helper method example:

protected RedirectResult RedirectToCurrentUri(String strQueryStringOverride = "")
{
    String strReferrer = Request.UrlReferrer.AbsoluteUri;

    if (String.IsNullOrWhiteSpace(strReferrer))
    {
        strReferrer = "/";
    }

    // Can also override referrer here for instances where page has 
    // refreshed and replaced referrer with current url.

    if (!String.IsNullOrWhiteSpace(strQueryStringOverride))
    {
        String strPath = (strReferrer ?? "").Split('?', '#')[0];

        return Redirect(strPath + strQueryStringOverride);
    }

    return Redirect(strReferrer);
}

Note that the method allows Query String override.

This can be used as a helper method in any controller as per below:

Redirect without changing query string (if any):

return RedirectToCurrentUri()

Example query string override:

return RedirectToCurrentUri("?success=true")

Upvotes: 0

Krishan
Krishan

Reputation: 2487

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

Change your code like this

Upvotes: 0

Moaz
Moaz

Reputation: 11

  1. on Get like Edit(int? id)

    ViewBag.RefUrl = Request.UrlReferrer.ToString();
    
  2. on view @Html.Hidden("RefUrl");
  3. on post Edit(int id,string RefUrl)

    return Redirect(RefUrl);
    

Upvotes: -2

vidriduch
vidriduch

Reputation: 4863

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

Upvotes: 50

Fals
Fals

Reputation: 6839

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.

Upvotes: 17

Felipe Oriani
Felipe Oriani

Reputation: 38618

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.

Upvotes: 15

Esteban Elverdin
Esteban Elverdin

Reputation: 3582

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

Upvotes: 10

Related Questions