zoukankan      html  css  js  c++  java
  • .Core中使用Session

    .Core中使用Session

    1.首先你需要安装NuGet包:Microsoft.AspNetCore.Session

    2.在Startup.cs文件中添加代码。

            public void ConfigureServices(IServiceCollection services)
            {
                services.AddSession();
    
                services.AddControllersWithViews();
                services.Configure<CookiePolicyOptions>(options => {
                    // This lambda determines whether user consent for non-essential cookies is needed for a given request.
                    options.CheckConsentNeeded = context => false;//默认为true,改为false
                    options.MinimumSameSitePolicy = SameSiteMode.None;
                });
            }
    public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
            {
                app.UseSession();
                if (env.IsDevelopment())
                {
                    app.UseDeveloperExceptionPage();
                }
                else
                {
                    app.UseExceptionHandler("/Home/Error");
                    // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
                    app.UseHsts();
                }
                app.UseHttpsRedirection();
                app.UseStaticFiles();
    
                app.UseRouting();
    
                app.UseAuthorization();
    
                app.UseEndpoints(endpoints =>
                {
                    endpoints.MapControllerRoute(
                        name: "default",
                        pattern: "{controller=Home}/{action=Index}/{id?}");
                });
            }

    Session写入

    HttpContext.Session.SetString("key", "value");

    Session读取

    HttpContext.Session.GetString("key")
  • 相关阅读:
    域名系统
    DNS域名解析过程
    服务器常用的状态码
    重绘与重排及它的性能优化
    console.time和console.timeEnd用法
    用CSS开启硬件加速来提高网站性能
    公钥和私钥
    svn conflict 冲突解决
    svn分支开发与主干合并(branch & merge)
    源生js惯性滚动与回弹效果
  • 原文地址:https://www.cnblogs.com/mvpbest/p/13679977.html
Copyright © 2011-2022 走看看