让我们假设我有一些页面

  • some.web/articles/details/5
  • some.web/users/info/bob
  • some.web/foo/bar/7

那能叫一个共同的实用控制器喜欢

locale/change/esauthorization/login

我如何获得这些方法(change, login)重定向到以前的行动(details, info, bar),而通过以前的参数,以他们(5, bob, 7)?

在短:我如何重新定向到的页面,我刚刚访问之后执行的行动在另一个控制器吗?

有帮助吗?

解决方案

尝试:

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"]);
}

其他提示

如果你想要重新定向,从一个按钮在看你可以使用:

@Html.ActionLink("Back to previous page", null, null, null, new { href = Request.UrlReferrer})

如果你不关心单元的测试,然后你可以简单地写入:

return Redirect(ControllerContext.HttpContext.Request.UrlReferrer.ToString());

一个建议如何做到这一点,例如说:

  1. 回url生存的一种形式的职位的请求(和任何失败的验证)
  2. 回url是确定从初始转诊url
  3. 不使用TempData[]或其他服务器的端状态
  4. 处理直接导航的行动(通过提供默认的重新定向)

.

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>
<% } %>

在视使用普通html 鉴页 java脚本果

<input type="button" value="GO BACK" class="btn btn-primary" 
onclick="location.href='@Request.UrlReferrer'" />

这个伟大工程。希望帮助别人。

@JuanPieterse已经回答了使用 @Html.ActionLink 因此,如果可能的人可以发表评论或回答使用 @Url.Action

通过一个returnUrl参数(url编码)的 改变登录 行动和内部的重新定向,以此给予returnUrl.您的登陆行动可能看起来是这样的:

public ActionResult Login(string returnUrl) 
{
    // Do something...
    return Redirect(returnUrl);
}

我使用。净核心2视,并且这一工作对我来说, 在使用控制器 HttpContext.Request.Headers["Referer"];

你可以回到以前的网页通过使用 ViewBag.ReturnUrl 财产。

动态造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 (一样的,但从内的任何控制行动)

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top