How do I store an object in Session in Asp.net core Web api?

0
       //
      public class OrderVM
      {
       public User numUsu { get; set; } 
        //La lista es de ProductOrder
       public  List<ProductOrder> Products  {get; set;}

       }

        var orderView = new OrderVM();
        orderView.numUsu = new User();
        orderView.Products = new List<ProductOrder>();


              var productOrder = new ProductOrder
            {
                Code = prod,
                AplhaNumericCode = produ.AplhaNumericCode,
                Description = produ.Description,
                Name = produ.Name,
                WholesalePrice = produ.WholesalePrice,
                idProd=produ.idProd,
                quant = cant
            };

       orderView.Products.Add(productOrder);
    
asked by Luis 22.04.2017 в 00:20
source

1 answer

1

If you find yourself within a Controller you can do it with the HttpContext.Session.Set<T>(key, data); method, assuming we want to add the orderView object:

string keyOrderView = "orderView";
HttpContext.Session.Set<OrderVM>(keyOrderView, orderView);

Now, to read the information of that session variable is with the HttpContext.Session.Get<T>(key); method:

OrderVM ordenes = HttpContext.Session.Get<OrderVM>(keyOrderView);

In this official Microsoft document ( in English ) explains how to handle the state and session of the application in ASP.NET Core.

    
answered by 22.04.2017 в 02:15