read connectionstring outside startup from appsetting.json in vNext

后端 未结 5 894
终归单人心
终归单人心 2020-12-14 02:52

I have a project class (Nuget Package). I need to read in a static class without constructor my connections string to MongoDB.

Static Class Method:

5条回答
  •  感情败类
    2020-12-14 03:07

    None of the above answers worked for me. I am building a .net core 2.1 api.

    I used

    public class Startup 
    {
        public static string ConnectionString {get; private set;}
    
    
    public void Configure(IApplicationBuilder app, IHostingEnvironment env)
            {
               if (env.IsDevelopment())
                {
                    app.UseDeveloperExceptionPage();
                }
                else
                {
                    app.UseHsts();
                }
                ConnectionString = Configuration.GetConnectionString("xxxxxx");
                app.UseMvc();
    }
    

    Then whenever i need it i call string ConnString = Startup.ConnectionString;

提交回复
热议问题