Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting the session Id from within a view component

This piece of code works fine to get the session Id from within the controller:

HttpContext.Session.SetString("_Name", "MyStore");
string SessionId = HttpContext.Session.Id;

... but when I try to put the same code in a view component class, VS tells me that the name HttpContext.Session.SetString (or just HttpContext.Session, or just HttpContext) does not exist in the current context. I have using Microsoft.AspNetCore.Http; at the top of the class.

edit

Here is my view component class:

public class ShoppingCartViewComponent : ViewComponent
{
    private readonly MyStoreContext _context;

    public ShoppingCartViewComponent(MyStoreContext context)
    {
        _context = context;
    }

    // Initialize session to enable SessionId
    // THIS WON'T WORK:
    HttpContext.Session.SetString("_Name", "MyStore");
    string SessionId = HttpContext.Session.Id;

    public async Task<IViewComponentResult> InvokeAsync(int Id)
    {
        var cart = await GetCartAsync(Id);
        return View(cart);
    }

    private async Task<ViewModelShoppingCart> GetCartAsync(int Id)
    {
        var VMCart = await _context.ShoppingCarts
                    .Where(c => c.Id == Id)
                    .Select(cart => new ViewModelShoppingCart
                    {
                        Id = cart.Id,
                        Title = cart.Title,
                        CreateDate = cart.CreateDate,
                        ShoppingCartItems = cart.ShoppingCartItems
                                            .Select(items => new ViewModelShoppingCartItem
                                            {
                                                ProductId = items.ProductId,
                                                ProductTitle = items.Product.Title,
                                                ProductPrice = items.Product.Price,
                                                Quantity = items.Quantity
                                            }).ToList()
                    }).FirstOrDefaultAsync();
        return VMCart;
    }
}
like image 437
Stian Avatar asked Feb 11 '26 17:02

Stian


1 Answers

The problem is that you are trying to access a method that exists on an instance of HttpContext, not a static method. The easiest way is to let the depenancy injection framework give you a IHttpContextAccessor. For example:

public class ShoppingCartViewComponent : ViewComponent
{
    private readonly IHttpContextAccessor _contextAccessor;
    private readonly MyStoreContext _context;

    public ShoppingCartViewComponent(MyStoreContext context,
        IHttpContextAccessor contextAccessor)
    {
        _context = context;
        _contextAccessor = contextAccessor;

        _contextAccessor.HttpContext.Session.SetString("_Name", "MyStore");
        string SessionId = _contextAccessor.HttpContext.Session.Id;
    }

    //snip rest of code for brevity    
}
like image 94
DavidG Avatar answered Feb 13 '26 14:02

DavidG



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!