Answer the question
In order to leave comments, you need to log in
When using signalR, it throws a System.InvalidOperationException: "The session cannot be established after the response has started."?
when you click on the "Add to Cart" button, the script is launched:
$('#AddCart').on('click', function (event) {
var productId = $('#userInput').val();
var quantityInProduct = $('input[name="quantityInProduct"][data-id="' + productId + '"]').val();
connection.invoke("QuantityOfCart", productId, quantityInProduct).catch(function (err) {
return console.error(err.toString());
});
});
public async Task QuantityOfCart(int productId, int quantityInProduct)
{
Product product = repositoryProduct.Products.FirstOrDefault(p => p.ProductID == productId);
if (product != null){cart.AddItem(product, quantityInProduct);}
int quantityOfCart = cart.Lines.Count();
await Clients.All.SendAsync("SendQuantityOfCart", quantityOfCart);}
public static void SetJson(this ISession session, string key, object value)
{
session.SetString(key, JsonConvert.SerializeObject(value));
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question