1首先引用sessionNuget包
2在startup.cs找到方法ConfigureServices(IServiceCollection services) 注入Session(这个地方是Asp.net Core pipeline):services.AddSession();
接下来我们要告诉Asp.net Core使用内存存储Session数据,在Configure(IApplicationBuilder app,...)中添加代码:app.UseSession();
3在homecontroller中实现
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
public IActionResult Index()
{
HttpContext.Session.SetString("code", "你好我是session");
ViewBag.Code = HttpContext.Session.GetString("code");
return View();
}
4实现效果