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