【问题标题】:ajax.actionlink shows different urls on server and visual studioajax.actionlink 在服务器和 Visual Studio 上显示不同的 url
【发布时间】:2011-12-04 08:11:25
【问题描述】:

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

【问题讨论】:

  • 最初该文件具有构建操作“编译”,这意味着该文件在服务器上不存在,但我现在已经使用构建操作“内容”进行了测试,以确保它确实是相同的。还是不行。
  • 好的,所以 routeprovider.cs 在 live 和 prod 上肯定是一样的,并且肯定已经复制到您的生产服务器上?
  • 可以请您发布您的操作方法吗?
  • 我现在已经添加了action方法。
  • 你能告诉我你的生产服务器设置,iis6 还是 7。你是如何手动发布的?

标签: asp.net-mvc-3 actionlink nopcommerce


【解决方案1】:

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

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

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

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

【讨论】:

  • 我已经编辑了原始问题,我错误地写了错误的URL。
  • 很酷,但看起来开发和生产上的路由仍然不同。您可以从 RegisterRoutes 发布路由规则吗?
  • 我发布了路由规则。为了清楚起见,我将其添加到原始问题中。不幸的是,我无法添加所有内容,但我将所有规则都添加到了不工作的规则中。
  • 开发和生产中的 routeprovider.cs 是否相同?
【解决方案2】:

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

【讨论】:

    【解决方案3】:
    @Ajax.ActionLink("-",
                "RemoveQuantityFromProduct",
                "ShoppingCart",
                new { productId = item.Id.ToString() },
                new AjaxOptions
                {
                    UpdateTargetId = "AjaxMiniShoppingCart",
                    HttpMethod = "POST",
                    InsertionMode = InsertionMode.Replace,
                    OnSuccess="toggleCart",
                }, null)
    

    尝试将 HTML 属性设置为 null。

    【讨论】:

    • 不幸的是,这没有帮助。本地电脑和服务器上的链接还是不一样,在服务器上还是不行。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-12-12
    • 2013-05-17
    • 1970-01-01
    • 2015-02-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多