Session的使用对于Cookie来说简单了不少
在.Net Core中一般都是先注册然后Use,这个也一样
在Startup的ConfigureServices方法中
//session
services.AddSession();
然后在Configure方法中
app.UseSession();//中间件使用session
这样就可以使用了
如果未配置会报错:System.InvalidOperationException:“Session has not been configured for this application or request.”那就是为进行注册
在MVC Controller里使用HttpContext.Session
需引入using Microsoft.AspNetCore.Http;
才可以使用HttpContext.Session.SetString(“user”, “sammy”);不然无SetString方法只有Set方法