VS2022 MVC和WebApi使用Session的细节。
在NETFW6.0 MVC框架中使用session添加两行代码即可。
builder.Services.AddSession();
app.UseSession();
在WebAPI框架中:
多加一行
builder.Services.AddDistributedMemoryCache();
builder.Services.AddSession();
app.UseSession();
搞定……
.相关错误提示:
System.AggregateException:“Some services are not able to be constructed (Error while validating the service descriptor ‘ServiceType: Microsoft.AspNetCore.Session.ISessionStore Lifetime: Transient ImplementationType: Microsoft.AspNetCore.Session.DistributedSessionStore’: Unable to resolve service for type ‘Microsoft.Extensions.Caching.Distributed.IDistributedCache’ while attempting to activate ‘Microsoft.AspNetCore.Session.DistributedSessionStore’.)”