Asp.net core中使用session
1. nuget包引用 microsoft.aspnetcore.session
2. startup.cs中的相应方法加入些代码:
public void configureservices(iservicecollection services)
{ //添加session
services.adddistributedmemorycache();
services.addsession();
services.addmvc();
}
public void configure(iapplicationbuilder app, ihostingenvironment env)
{
app.usesession(); //必须加上这句才能用session
}
3.控制器中的使用;
using microsoft.aspnetcore.http;
public iactionresult show()
{
request.httpcontext.session.setstring("date",datetime.now.tostring());//设置session
var tenantid = request.httpcontext.session.getstring("date");//获取session
return view();
}
推荐阅读