我正在尝试使用 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);
}