如何将连接字符串存储到ASP.NET Core中的会话?

时间:2017-11-30 22:24:35

标签: c# session asp.net-core asp.net-core-2.0

我是ASP.NET Core的新手,我想通过会话访问连接字符串,但我不确定我应该在何时设置会话值。

我可以访问Startup.cs中的连接字符串,但我想我无法从那里访问HttpContext

以下是 Startup.cs 文件

public class Startup
    {
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        public IConfiguration Configuration { get; }


        public void ConfigureServices(IServiceCollection services)
        {
            services.AddMvc();
            services.AddDistributedMemoryCache();
            services.AddSession(options =>
            {
                options.IdleTimeout = TimeSpan.FromMinutes(15);
            });

            // Here I can get connection string, but I cannot access Session
            var connection = Configuration.GetConnectionString("devDb2");
            services.AddDbContext<attWebApiContext>(options => options.UseSqlServer(connection));
        }


        public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }

            app.UseSession();
            app.UseMvc();                    
        }
    }

0 个答案:

没有答案