I am new to ASP.NET Core and I would like to access the connection string through the session but I am not sure at which point I should set the session value.
I can access connection string in Startup.cs but I think I cannot access HttpContext from there.
Here is the Startup.cs file
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();
}
}