使用nuget 添加引用 Microsoft.AspNetCore.Session
在nuget中执行命令 :Install-Package Microsoft.AspNetCore.Session
更新 Startup.cs 使用需要的服务
ublic void ConfigureServices(IServiceCollection services)
{
services.AddSession();
services.AddMvc();
}
在Configure中启用session
app.UseSession();
注意:当添加完
Microsoft.AspNetCore.Session依赖后很有可能在写 app.UseSession()
与services.AddSession()的时候会报错
其实生成是可以生成成功的,因为才添加了引用vs的智能提示还没反应过来,关闭重新运行一下就可以了
然后使用session
存:
HttpContext.Session.SetString("username","xx");
取:
string username = HttpContext.Session.GetString("username");
以前session中可以直接保存对象,这里还不行