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

    .NET Core中使用Session步骤如下:

    1、安装Microsoft.AspNetCore.Session    NuGet包

    2、修改Startup.cs 添加相关服务,services.AddSession()和app.UseSession()

    // 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");
  • 相关阅读:
    定位,标记,Socket通信传输位置
    多维数组的下标存取
    ufunc函数
    八大排序算法
    揭开Python科学计算的面纱
    【python51--__name__属性】
    【Python48--魔法方法:迭代器&生成器】
    【Python047-魔法方法:定制序列】
    【MonkeyRunner环境搭建】
    【Python046--魔法方法:描述符】
  • 原文地址:https://www.cnblogs.com/liushunli/p/10346403.html
Copyright © 2011-2022 走看看