【发布时间】:2020-06-05 19:12:37
【问题描述】:
我已将app.UseSession(); 添加到我的startup.Configure 并将services.AddSession() 添加到我的ConfigureServices。
现在,如果我尝试像这样使用 Session:
HttpContext.Session.SetString("Type", tableName);
我得到“非静态字段、方法或属性‘HttpContext.Session’需要对象引用”
但是,如果我尝试像这样实例化它:
HttpContext context = new HttpContext();
它说:“无法创建抽象类或接口'HttpContext”的实例
如何访问会话?
IQuery.cs
using Microsoft.AspNetCore.Http;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
namespace AppName.Services
{
public interface IQuery
{
List<Dictionary<string, string>> GetListOfDatabases(string dbName);
}
public class InMemoryIquery : IQuery
{
public List<Dictionary<string, string>> GetListOfDatabases(string tableName)
{
if(tableName != null)
{
HttpContext.Session.SetString("Type", tableName);
}
}
}
【问题讨论】:
-
您在什么情况下访问它?见Access HttpContext in ASP.NET Core
-
我正在尝试从继承接口的类中访问它
-
你能把完整的代码贴在你试图访问会话的地方吗?
-
如果您发送访问会话的类的完整代码,我们可以提供帮助。
-
我已经发布了上面的代码。谢谢
标签: c# asp.net session asp.net-core httpcontext