2

ajax.actionlink を使用して、ショッピングカート内のアイテムの数を削除しようとしています。ローカル コンピューター (Visual Studio 2010 を使用) では正常に動作しますが、サーバーでは正常に動作しない ajax.actionlink を追加しました。コードは次のとおりです。

@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 からコードを投稿すると、(運用と開発の両方で) さらに役立つ可能性があります。

タイプミスかもしれませんが、投稿のアクションリンクにはremovequantityfromproductがありますが、貼り付けたURLにはaddquantitytoproductがあります.2つの異なるアクションです. そのため、アクション removequantityfromproduct と誤って一致し、addquantitytoproduct にリダイレクトする maproute がある可能性があります。

于 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 に答える