Home > Blockchain >  Cookies problem using web application asp net core
Cookies problem using web application asp net core

Time:10-21

I am working with a shopping cart application. I have developed an application that maintains user shopping carts using cookies. It was working perfectly until I have made some UI changes and now it is not working now I have no clue what I have done wrong because I am maintaining the cookies through c# backend code no interruption with the front end only reading these cookies and passing them into view model to show on cart panel. Here is my code

Startup.cs

            app.UseRouting();
            app.UseCookiePolicy();
            app.UseAuthentication();

Adding Items to Cart

public JsonResult AddToShoppingCart(UserProductVM model)
        {
            try
            {
                if (string.IsNullOrEmpty(model.SelectedSize))
                    return Json(new { status = false, msg = "Please select size to proceed." });
                var result = ShoppingCartHelper.GetShoppingCartList(model, _httpContextAccessor);
                if (result.Status <= 0)
                    return Json(new { status = false, msg = result.Message });
                Response.Cookies.Delete("ShoppingCart");
                Response.Cookies.Append("ShoppingCart", JsonConvert.SerializeObject(result.Data));
                return Json(new { status = true, msg = "Success! added to shopping cart." });
            }
            catch (Exception ex)
            {
                return Json(new { status = false, msg = ex.Message.ToString() });
            }
        }

Reading From Cart

public static string GetShoppingCartFromCookies(IHttpContextAccessor _httpContextAccessor)
        {
            return _httpContextAccessor.HttpContext?.Request?.Cookies["ShoppingCart"]?.ToString();
        }

Every Thing was working fine and now nothing works no cookies are added to the cookies list. This is the same code I have also the backup my application and when I run that application cookies are working perfectly but the problem is that it is with the old design I am not using that UI design anymore. This is the same code working in one application but not working with another application with a different UI.

CodePudding user response:

Have you added the IHttpContextAccessor to dependency container ?

services.TryAddSingleton<IHttpContextAccessor, HttpContextAccessor>();

CodePudding user response:

I have resolved my issue. The problem was basically I was adding base64 image string which was not working. When I comment on this line it worked perfectly no need to change any code.

 public static GenericResponseDTO<List<ShoppingCartViewModel>> GetShoppingCartList(UserProductVM model, IHttpContextAccessor _httpContextAccessor)
        {
            var shoppingcartmodel = new List<ShoppingCartViewModel>();
            var cartmodel = new ShoppingCartViewModel
            {
                RestaurantId = model.RestaurantId,
                SubCategoryId = model.SubCategoryId,
                RestaurantSubCategoryId = model.RestaurantSubCategoryId,
                OrderGuid = Guid.NewGuid(),
                Quantity = model.Quantity,
                //ItemImage = model.ItemImage,
                SelectedSize = model.SelectedSize.Split('-')[0],
                SingleItemPrice = Convert.ToDecimal(model.SelectedSize.Split('-')[1]),
                SubCategoryName = model.SubCategoryName,
                TotalItemPrice = (model.Quantity * Convert.ToDecimal(model.SelectedSize.Split('-')[1]))
            };
            if (string.IsNullOrEmpty(GetShoppingCartFromCookies(_httpContextAccessor)))
                shoppingcartmodel.Add(cartmodel);
            else
            {
                shoppingcartmodel = JsonConvert.DeserializeObject<List<ShoppingCartViewModel>>(GetShoppingCartFromCookies(_httpContextAccessor));
                if (shoppingcartmodel.Any(x => x.RestaurantSubCategoryId == model.RestaurantSubCategoryId))
                    return new GenericResponseDTO<List<ShoppingCartViewModel>> { Data = new List<ShoppingCartViewModel>(), Status = -1, Message = "Item is alreay exists in your cart please remove and add another." };
                shoppingcartmodel.Add(cartmodel);
            }
            return new GenericResponseDTO<List<ShoppingCartViewModel>> { Data = shoppingcartmodel, Status = 1 };
        }

I have commented on the ItemImage line and it works like a charm. Maybe it helps someone else problem.

  • Related