.net core MVC和WebApi使用Session的方法
小白浏览:7352023-12-27 15:07:26本文累计收益:0我也要赚钱

在NETFW6.0 MVC框架中使用session添加两行代码即可。

builder.Services.AddSession();
app.UseSession();

在WebAPI框架中:
多加一行

builder.Services.AddDistributedMemoryCache();
builder.Services.AddSession();
builder.Services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
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’.)”
 

评论列表
发表评论
+ 关注