0

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();                    
        }
    }
8
  • 4
    Why on earth would you do that? It doesn't change. Commented Nov 30, 2017 at 22:25
  • I want to get connection string and use it inside application (Not necessarilly pass it through session) Commented Nov 30, 2017 at 22:27
  • 2
    But you created a DbContext with that connectionstring. What else do you need if you have already a DbContext to Inject in your classes? Commented Nov 30, 2017 at 22:36
  • I want to execute sql queries using SqlCommand and connection strings apart from using DbContext Commented Nov 30, 2017 at 22:40
  • 1
    @kapantzak Access Connection String inside an ASP.NET Core controller has answer to your question Commented Nov 30, 2017 at 22:44

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.