2

我正在尝试使用 ajax.actionlink 删除购物车中的商品数量。我添加了一个 ajax.actionlink,它在我的本地计算机(使用 Visual Studio 2010)上运行良好,但在我的服务器上却不行。这是代码:

@Ajax.ActionLink("-",
            "RemoveQuantityFromProduct",
            "ShoppingCart",
            new { productId = item.Id.ToString() },
            new AjaxOptions
            {
                UpdateTargetId = "AjaxMiniShoppingCart",
                HttpMethod = "POST",
                InsertionMode = InsertionMode.Replace,
                OnSuccess="toggleCart",
            })

在我的本地计算机上,生成的链接是: http://localhost:2565/shoppingcart/removequantityfromproduct/16

然而,在我的服务器上,结果链接是:http: //www.domain.com/shoppingcart/removequantityfromproduct?productId=16

两个链接都可以在本地计算机上运行,​​但两个链接都会在服务器上导致 404 错误。

有谁知道为什么服务器和本地计算机上的 url 不同?谁能解释为什么路由在本地计算机上有效但在服务器上无效?

(我使用 nopCommerce 2.1 作为这个网上商店的基础。)

[编辑:更正了生成的 URL:s。我输入了添加产品的 URL,而不是删除产品的 URL。] 2011-10-10 11:23

[编辑从 NopCommerce 添加了 RouteProvider.cs] 2011-10-10 11:30

            //home page
            routes.MapLocalizedRoute("HomePage",
                            "",
                            new { controller = "Home", action = "Index"},
                            new[] { "Nop.Web.Controllers" });

            //products
            routes.MapLocalizedRoute("Product",
                            "p/{productId}/{SeName}",
                            new { controller = "Catalog", action = "Product", SeName = UrlParameter.Optional },
                            new { productId = @"\d+" },
                            new[] { "Nop.Web.Controllers" });
            routes.MapLocalizedRoute("RecentlyViewedProducts",
                            "recentlyviewedproducts/",
                            new { controller = "Catalog", action = "RecentlyViewedProducts" },
                            new[] { "Nop.Web.Controllers" });
            routes.MapLocalizedRoute("RecentlyAddedProducts",
                            "newproducts/",
                            new { controller = "Catalog", action = "RecentlyAddedProducts" },
                            new[] { "Nop.Web.Controllers" });
            routes.MapLocalizedRoute("RecentlyAddedProductsRSS",
                            "newproducts/rss",
                            new { controller = "Catalog", action = "RecentlyAddedProductsRss" },
                            new[] { "Nop.Web.Controllers" });

            //comparing products
            routes.MapLocalizedRoute("AddProductToCompare",
                            "compareproducts/add/{productId}",
                            new { controller = "Catalog", action = "AddProductToCompareList" },
                            new { productId = @"\d+" },
                            new[] { "Nop.Web.Controllers" });
            routes.MapLocalizedRoute("CompareProducts",
                            "compareproducts/",
                            new { controller = "Catalog", action = "CompareProducts" },
                            new[] { "Nop.Web.Controllers" });
            routes.MapLocalizedRoute("RemoveProductFromCompareList",
                            "compareproducts/remove/{productId}",
                            new { controller = "Catalog", action = "RemoveProductFromCompareList"},
                            new[] { "Nop.Web.Controllers" });
            routes.MapLocalizedRoute("ClearCompareList",
                            "clearcomparelist/",
                            new { controller = "Catalog", action = "ClearCompareList" },
                            new[] { "Nop.Web.Controllers" });

            //product email a friend
            routes.MapLocalizedRoute("ProductEmailAFriend",
                            "productemailafriend/{productId}",
                            new { controller = "Catalog", action = "ProductEmailAFriend" },
                            new { productId = @"\d+" },
                            new[] { "Nop.Web.Controllers" });

            //catalog
            routes.MapLocalizedRoute("Category",
                            "c/{categoryId}/{SeName}",
                            new { controller = "Catalog", action = "Category", SeName = UrlParameter.Optional },
                            new { categoryId = @"\d+" },
                            new[] { "Nop.Web.Controllers" });
            routes.MapLocalizedRoute("ManufacturerList",
                            "manufacturer/all/",
                            new { controller = "Catalog", action = "ManufacturerAll" },
                            new[] { "Nop.Web.Controllers" });
            routes.MapLocalizedRoute("Manufacturer",
                            "m/{manufacturerId}/{SeName}",
                            new { controller = "Catalog", action = "Manufacturer", SeName = UrlParameter.Optional },
                            new { manufacturerId = @"\d+" },
                            new[] { "Nop.Web.Controllers" });

            //reviews
            routes.MapLocalizedRoute("ProductReviews",
                            "productreviews/{productId}",
                            new { controller = "Catalog", action = "ProductReviews" },
                            new[] { "Nop.Web.Controllers" });

            //login, register
            routes.MapLocalizedRoute("Login",
                            "login/",
                            new { controller = "Customer", action = "Login" },
                            new[] { "Nop.Web.Controllers" });
            routes.MapLocalizedRoute("LoginCheckoutAsGuest",
                            "login/checkoutAsGuest",
                            new { controller = "Customer", action = "Login", checkoutAsGuest = true },
                            new[] { "Nop.Web.Controllers" });
            routes.MapLocalizedRoute("Register",
                            "register/",
                            new { controller = "Customer", action = "Register" },
                            new[] { "Nop.Web.Controllers" });
            routes.MapLocalizedRoute("Logout",
                            "logout/",
                            new { controller = "Customer", action = "Logout" },
                            new[] { "Nop.Web.Controllers" });
            routes.MapLocalizedRoute("RegisterResult",
                            "registerresult/{resultId}",
                            new { controller = "Customer", action = "RegisterResult" },
                            new { resultId = @"\d+" },
                            new[] { "Nop.Web.Controllers" });


            //shopping cart
            routes.MapLocalizedRoute("AddProductToCart",
                            "cart/addproduct/{productId}",
                            new { controller = "ShoppingCart", action = "AddProductToCart" },
                            new { productId = @"\d+" },
                            new[] { "Nop.Web.Controllers" });
            routes.MapLocalizedRoute("ShoppingCart",
                            "cart/",
                            new { controller = "ShoppingCart", action = "Cart" },
                            new[] { "Nop.Web.Controllers" });
            routes.MapLocalizedRoute("AjaxShoppingCart",
                            "cart/",
                            new { controller = "ShoppingCart", action = "Cart" },
                            new[] { "Nop.Web.Controllers" });
            routes.MapLocalizedRoute("AddQuantity",
                            "shoppingcart/addquantitytoproduct/{productId}",
                            new { controller = "ShoppingCart", action = "AddQuantityToProduct", productId = UrlParameter.Optional },
                            new[] { "Nop.Web.Controllers" });
            routes.MapLocalizedRoute("RemoveQuantity",
                            "shoppingcart/removequantityfromproduct/{productId}",
                            new { controller = "ShoppingCart", action = "RemoveQuantityFromProduct", productId = UrlParameter.Optional },
                            new[] { "Nop.Web.Controllers" });

动作方法:

public ActionResult RemoveQuantityFromProduct(int productId)
{
    Response.CacheControl = "no-cache";
    Response.Cache.SetETag((Guid.NewGuid()).ToString());
    var cart = _workContext.CurrentCustomer.ShoppingCartItems.Where(sci => sci.ShoppingCartType == ShoppingCartType.ShoppingCart).ToList();

    foreach (var sci in cart)
    {
        if (productId.Equals(sci.Id))
        {
            if (sci.Quantity > 1)
            {
                _shoppingCartService.UpdateShoppingCartItem(_workContext.CurrentCustomer, sci.Id, sci.Quantity - 1, true);
            }
            else
            {
                _shoppingCartService.DeleteShoppingCartItem(sci, true);
            }
        }
    }

    //updated cart
    cart = _workContext.CurrentCustomer.ShoppingCartItems.Where(sci => sci.ShoppingCartType == ShoppingCartType.ShoppingCart).ToList();
    var model = PrepareShoppingCartModel(new MiniShoppingCartModel(), cart, true);
    return PartialView("AjaxMiniShoppingCart", model);
}
4

3 回答 3

2

我的猜测是您的本地服务器和生产服务器之间的路由不同。在您的本地计算机上,global.asax.cs 中的路由与 url 和路由正确匹配。

在您的生产服务器上,我认为它与路由不匹配。所以我建议你检查本地和生产是否同步。

如果您从 RegisterRoutes -> Global.asax.cs 发布您的代码可能会提供进一步的帮助(生产和开发)。

这可能是一个错字,但在您的帖子中,actionlink 具有 removequantityfromproduct 但您粘贴的 url 具有 addquantitytoproduct - 两个不同的操作。因此,您的 maproute 可能不正确地匹配操作 removequantityfromproduct 并重定向到 addquantitytoproduct。

于 2011-10-10T09:08:17.143 回答
2

这个问题的答案很简单......我在发布模式下构建了解决方案,但由于某种原因,部署脚本从调试文件夹中获取了一些代码,这就是路由不正确的原因......

于 2011-10-13T06:39:39.697 回答
1
@Ajax.ActionLink("-",
            "RemoveQuantityFromProduct",
            "ShoppingCart",
            new { productId = item.Id.ToString() },
            new AjaxOptions
            {
                UpdateTargetId = "AjaxMiniShoppingCart",
                HttpMethod = "POST",
                InsertionMode = InsertionMode.Replace,
                OnSuccess="toggleCart",
            }, null)

尝试将 HTML 属性设置为 null。

于 2011-10-10T08:29:05.770 回答