我需要在我的Controller中的其他库中保留特定对象的状态,但我已经按照一些tutos教授如何在Session
中使用HttpContext
,但是{{1} }在控制器的构造函数中为null
中间件全部设置完毕:
HttpContext
在这种情况下,我正在尝试最简单的事情:
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc();
services.AddDistributedMemoryCache();
services.AddSession();
services.AddDbContext<AutoListDbContext>(options => options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
//(...)
app.UseStaticFiles();
app.UseSession();
app.UseMvc(routes =>
//(...)
}
但 public AddDoorController(AutoListDbContext context)
{
var building = new Building(context);
HttpContext.Session.SetString("building", JsonConvert.SerializeObject(building));
}
(继承自HttpContext
)为空。我错过了什么?