我是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();
}
}