【问题标题】:Getting the session Id from within a view component从视图组件中获取会话 ID
【发布时间】:2018-06-30 07:33:48
【问题描述】:

这段代码可以很好地从控制器中获取会话 ID:

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

...但是当我尝试将相同的代码放在视图组件类中时,VS 告诉我名称 HttpContext.Session.SetString(或只是 HttpContext.Session,或只是 HttpContext)在当前上下文中不存在.我有using Microsoft.AspNetCore.Http; 在班级顶部。

编辑

这是我的视图组件类:

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;
    }
}

【问题讨论】:

  • 缺少哪位? HttpContext 自己工作吗? HttpContext.Session 怎么样?
  • @DavidG 以上都不是。

标签: c# session asp.net-core asp.net-core-viewcomponent


【解决方案1】:

问题是您试图访问存在于HttpContext 实例上的方法,而不是静态方法。最简单的方法是让依赖注入框架给你一个IHttpContextAccessor。例如:

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    
}

【讨论】:

  • 我很抱歉在这里完全是个菜鸟,但我正在学习 c# 和 asp.net。我不明白我应该如何在我的代码中实现你的建议。我已经更新了我的问题以包括整个视图组件类。我应该在哪里以及如何设置 SessionId 字符串的值?
  • 我已经尽可能地更新了代码以匹配您的代码。您实际在哪里设置会话 ID 取决于您,但这会告诉您如何操作。
  • @DavidG 很好的例子,现在尝试在 20 年使用它,您是否必须在任何地方注入它以便将当前会话传递给 contextAccessor?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-02-17
  • 2011-09-07
  • 1970-01-01
  • 1970-01-01
  • 2020-11-04
  • 2018-08-05
  • 1970-01-01
相关资源
最近更新 更多