Answers:
尝试:
public ActionResult MyNextAction()
{
return Redirect(Request.UrlReferrer.ToString());
}
或者,触摸达林所说的,尝试以下操作:
public ActionResult MyFirstAction()
{
return RedirectToAction("MyNextAction",
new { r = Request.Url.ToString() });
}
然后:
public ActionResult MyNextAction()
{
return Redirect(Request.QueryString["r"]);
}
Request.Url.AbsolutePath.ToString()
的AccountController
的LogOn
方法包含的URL年初检查'/'
等
GET Index
那么GET Edit
。然后,引用URL是,Index
但是当用户执行POST Edit
引用时,引用URL 现在Edit
来自先前的GET请求。我如何确定POST Edit
知道引荐用户的URL GET Edit
?
关于如何做到这一点的建议:
。
public ActionResult Create(string returnUrl)
{
// If no return url supplied, use referrer url.
// Protect against endless loop by checking for empty referrer.
if (String.IsNullOrEmpty(returnUrl)
&& Request.UrlReferrer != null
&& Request.UrlReferrer.ToString().Length > 0)
{
return RedirectToAction("Create",
new { returnUrl = Request.UrlReferrer.ToString() });
}
// Do stuff...
MyEntity entity = GetNewEntity();
return View(entity);
}
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Create(MyEntity entity, string returnUrl)
{
try
{
// TODO: add create logic here
// If redirect supplied, then do it, otherwise use a default
if (!String.IsNullOrEmpty(returnUrl))
return Redirect(returnUrl);
else
return RedirectToAction("Index");
}
catch
{
return View(); // Reshow this view, with errors
}
}
您可以在视图中使用重定向,如下所示:
<% if (!String.IsNullOrEmpty(Request.QueryString["returnUrl"])) %>
<% { %>
<a href="<%= Request.QueryString["returnUrl"] %>">Return</a>
<% } %>
将returnUrl参数(URL编码)传递给更改和登录操作,并在内部重定向到此给定的returnUrl。您的登录操作可能如下所示:
public ActionResult Login(string returnUrl)
{
// Do something...
return Redirect(returnUrl);
}
我正在使用.Net Core 2 MVC,而这对我来说很有效,在控制器中使用
HttpContext.Request.Headers["Referer"];
要在任何视图中动态构造returnUrl,请尝试以下操作:
@{
var formCollection =
new FormCollection
{
new FormCollection(Request.Form),
new FormCollection(Request.QueryString)
};
var parameters = new RouteValueDictionary();
formCollection.AllKeys
.Select(k => new KeyValuePair<string, string>(k, formCollection[k])).ToList()
.ForEach(p => parameters.Add(p.Key, p.Value));
}
<!-- Option #1 -->
@Html.ActionLink("Option #1", "Action", "Controller", parameters, null)
<!-- Option #2 -->
<a href="/Controller/Action/@object.ID?returnUrl=@Url.Action(ViewContext.RouteData.Values["action"].ToString(), ViewContext.RouteData.Values["controller"].ToString(), parameters)">Option #2</a>
<!-- Option #3 -->
<a href="@Url.Action("Action", "Controller", new { object.ID, returnUrl = Url.Action(ViewContext.RouteData.Values["action"].ToString(), ViewContext.RouteData.Values["controller"].ToString(), parameters) }, null)">Option #3</a>
这在“布局页面”,“部分视图”和“ HTML帮助器”中也适用
相关:MVC3动态返回URL(相同,但来自任何Controller / Action中)
对于ASP.NET Core, 可以使用asp-route- *属性:
<form asp-action="Login" asp-route-previous="@Model.ReturnUrl">
其他详细信息示例: 假设您有一个带操作的车辆控制器
指数
细节
编辑
并且您可以从“索引”或“详细信息”编辑任何车辆,因此,如果单击“从索引编辑”,则必须在编辑后返回索引;如果您单击“从细节编辑”,则必须在编辑后返回细节。
//In your viewmodel add the ReturnUrl Property
public class VehicleViewModel
{
..............
..............
public string ReturnUrl {get;set;}
}
Details.cshtml
<a asp-action="Edit" asp-route-previous="Details" asp-route-id="@Model.CarId">Edit</a>
Index.cshtml
<a asp-action="Edit" asp-route-previous="Index" asp-route-id="@item.CarId">Edit</a>
Edit.cshtml
<form asp-action="Edit" asp-route-previous="@Model.ReturnUrl" class="form-horizontal">
<div class="box-footer">
<a asp-action="@Model.ReturnUrl" class="btn btn-default">Back to List</a>
<button type="submit" value="Save" class="btn btn-warning pull-right">Save</button>
</div>
</form>
在您的控制器中:
// GET: Vehicle/Edit/5
public ActionResult Edit(int id,string previous)
{
var model = this.UnitOfWork.CarsRepository.GetAllByCarId(id).FirstOrDefault();
var viewModel = this.Mapper.Map<VehicleViewModel>(model);//if you using automapper
//or by this code if you are not use automapper
var viewModel = new VehicleViewModel();
if (!string.IsNullOrWhiteSpace(previous)
viewModel.ReturnUrl = previous;
else
viewModel.ReturnUrl = "Index";
return View(viewModel);
}
[HttpPost]
public IActionResult Edit(VehicleViewModel model, string previous)
{
if (!string.IsNullOrWhiteSpace(previous))
model.ReturnUrl = previous;
else
model.ReturnUrl = "Index";
.............
.............
return RedirectToAction(model.ReturnUrl);
}