C# ASP.NET MVC 4 - 控制器结束后重定向到同一页面
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18877883/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
ASP.NET MVC 4 - Redirect to the same page after controller ends
提问by Ralph
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?
我的问题是 returnUrl 为 null 并且它不会重定向到调用控制器的同一页面。想法?
Using: ASP.NET MVC 4 Razor
使用:ASP.NET MVC 4 Razor
采纳答案by Felipe Oriani
You could use a Request.QueryString
method to get some values from URL, for sample:
您可以使用一种Request.QueryString
方法从 URL 获取一些值,例如:
@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
.
在您的控制器中,将其作为参数接收string returnUrl
。
回答by Esteban Elverdin
in your controller class use Request.UrlReferrer
. There's no need to pass the url from the page.
在您的控制器类中使用Request.UrlReferrer
. 无需从页面传递 url。
public ActionResult AddEntry(string ip, int TypeId)
{
// Do some stuff
return Redirect(Request.UrlReferrer.ToString());
}
回答by Fals
You can get the Refer URL from the Request
in the controller:
您可以从Request
控制器中获取引用 URL :
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.
这会将您准确重定向到调用 URL。
回答by vidriduch
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);
回答by Moaz
on Get like
Edit(int? id)
ViewBag.RefUrl = Request.UrlReferrer.ToString();
- on view
@Html.Hidden("RefUrl");
on post
Edit(int id,string RefUrl)
return Redirect(RefUrl);
在获取喜欢
Edit(int? id)
ViewBag.RefUrl = Request.UrlReferrer.ToString();
- 正在查看
@Html.Hidden("RefUrl");
在岗位上
Edit(int id,string RefUrl)
return Redirect(RefUrl);
回答by Krishan
@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
像这样改变你的代码
回答by Luke Alderton
I found that using UrlReferrer works well and allows me to add on extra params if needed.
我发现使用 UrlReferrer 效果很好,并允许我在需要时添加额外的参数。
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")