【问题标题】:Return to current url in asp.net mvc在 asp.net mvc 中返回当前 url
【发布时间】:2012-02-20 20:15:16
【问题描述】:

我有一个方法:

public ActionResult AddProductToCart(int productId)
    {
        var product = _productService.GetProductById(productId);
        if (product == null)
            return RedirectToAction("Index", "Home");

        int productVariantId = 0;
        if (_shoppingCartService.DirectAddToCartAllowed(productId, out productVariantId))
        {
            var productVariant = _productService.GetProductVariantById(productVariantId);
            var addToCartWarnings = _shoppingCartService.AddToCart(_workContext.CurrentCustomer,
                productVariant, ShoppingCartType.ShoppingCart,
                string.Empty, decimal.Zero, 1, true);
            if (addToCartWarnings.Count == 0)
                //return RedirectToRoute("ShoppingCart");
            else
                return RedirectToRoute("Product", new { productId = product.Id, SeName = product.GetSeName() });
        }
        else
            return RedirectToRoute("Product", new { productId = product.Id, SeName = product.GetSeName() });
    }

您会看到被注释掉的行:我希望那里不触发任何重定向,而只是停留在发出此请求的同一页面上。

如果我输入return View() 就不好了,因为它会搜索具有此名称的视图,而此方法是添加到购物车的简单操作..

你能给我一个如何重定向到当前网址或留在同一页面上的解决方案吗?

【问题讨论】:

    标签: asp.net-mvc-3 razor


    【解决方案1】:

    您可以向此方法传递一个额外的 returnUrl 查询字符串参数,指示将产品添加到购物车后要返回的 URL:

    public ActionResult AddProductToCart(int productId, string returnUrl)
    

    这样您就可以重定向回原来的位置:

    if (addToCartWarnings.Count == 0)
    {
        // TODO: the usual checks that returnUrl belongs to your domain
        // to avoid hackers spoofing your users with fake domains
        if (!Url.IsLocalUrl(returnUrl))
        {
            // oops, someone tried to pwn your site => take respective actions
        } 
        return Redirect(returnUrl);
    }
    

    以及在生成此操作的链接时:

    @Html.ActionLink(
        "Add product 254 to the cart", 
        "AddProductToCart", 
        new { productId = 254, returnUrl = Request.RawUrl }
    )
    

    或者,如果您正在发布此操作(顺便说一下,您可能应该是因为它正在修改服务器上的状态 - 它将产品添加到购物车或其他东西):

    @using (Html.BeginForm("AddProductToCart", "Products"))
    {
        @Html.Hidden("returnurl", Request.RawUrl)
        @Html.HiddenFor(x => x.ProductId)
        <button type="submit">Add product to cart</button>
    }
    

    另一种可能性是使用 AJAX 来调用此方法。这样,用户在调用它之前就可以留在页面上。

    【讨论】:

    • 抓取引荐来源网址“简单”,我认为这是一个更好的解决方案。这是更多的前期工作,但我相信它会给你一个更可靠的结果。在滑动方面,您可以尝试使用returnUrl 并默认为Request.Referrer。一个更安全的方法是创建一个 HtmlExtender(可能是Html.ActionLinkWithReturnUrl(...)?)
    • @BradChristie,我个人不使用Referrer。不知道为什么。就是不喜欢。
    • 任何你在无状态环境中依赖事物的东西也让我感到不安;所以我和你在一起。
    • 不知道为什么,我只是偏执地认为必须有一些 IE 版本或在特定情况下不会随请求发送此 HTTP 标头的东西,它会破坏我的网站而使用查询字符串参数我觉得更安全。去怪IE,而不是我。它咬了我很多次,我更愿意采取预防措施。我没有严格的科学证据,只是我对 IE 的偏执。但也许这一切都不是必需的,Referrer 标头可以正常工作。
    • @BradChristie / Darin:请参阅the answers to this question,了解为什么依赖它是一个坏主意。如果在这种情况下没有发送,该站点仍然可以运行,但用户将被重定向到默认路由(我假设)。从可用性的角度来看,这可能会非常令人困惑,而且肯定是有害的。
    【解决方案2】:

    假设您的意思是返回到访问该控制器之前的位置:

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

    请记住,如果您发布到 [previous] 页面,您将不知所措,因为您没有模仿相同的请求。

    【讨论】:

      猜你喜欢
      • 2010-11-15
      • 1970-01-01
      • 2010-10-06
      • 2010-10-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-07-20
      • 2022-01-21
      相关资源
      最近更新 更多