Reputation: 10650
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
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
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
Reputation: 11
on Get like Edit(int? id)
ViewBag.RefUrl = Request.UrlReferrer.ToString();
@Html.Hidden("RefUrl");
on post Edit(int id,string RefUrl)
return Redirect(RefUrl);
Upvotes: -2
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
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
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
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