.NETCore中使⽤Session步骤.NET Core中使⽤Session步骤如下:
1、安装Microsoft.AspNetCore.Session    NuGet包
2、修改Startup.cs 添加相关服务,services.AddSession()和app.UseSession()session如何设置和读取
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
services.AddIdentity<ApplicationUser, IdentityRole>()
.AddEntityFrameworkStores<ApplicationDbContext>()
.AddDefaultTokenProviders();
/
/ Add application services.
services.AddTransient<IEmailSender, EmailSender>();
//Add session
services.AddSession();
services.AddMvc();
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
app.UseBrowserLink();
app.UseDatabaseErrorPage();
}
else
{
app.UseExceptionHandler("/Home/Error");
}
app.UseStaticFiles();
app.UseAuthentication();
app.UseSession();
app.UseMvc(routes =>
{
//Default route
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
});
}
3、Session ⽤法
引⽤
using Microsoft.AspNetCore.Http;
Session写⼊
HttpContext.Session.SetString("key", "value");
Session读取
HttpContext.Session.GetString("key");

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。