ASP.NET vNext global config access

后端 未结 2 780
清酒与你
清酒与你 2021-01-22 20:40

What is the correct/recommended way of accessing the config.json file (or wherever else config is stored) in ASP.NET vNext?

In the Startup clas

2条回答
  •  抹茶落季
    2021-01-22 21:01

    Here's what I've got that's working:

    Startup

    public class Startup
    {
        public IConfiguration Configuration { get; set; }
    
        public Startup(IHostingEnvironment env, IApplicationEnvironment appEnv)
        {
            // create & store the configuration once
            var configurationBuilder = new ConfigurationBuilder(appEnv.ApplicationBasePath)
                .AddJsonFile("config.json")
                .AddEnvironmentVariables();
    
            Configuration = configurationBuilder.Build();
        }
    
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddEntityFramework()
                .AddSqlServer()
                .AddDbContext(options => options.UseSqlServer(Configuration["Data:DefaultConnection:ConnectionString"]));
    
            services.AddIdentity()
                    .AddEntityFrameworkStores()
                    .AddDefaultTokenProviders();
    
            services.AddMvc();
    
            // adding/registering the dbContext for dependency injection as a singleton
            services.AddSingleton(s => new Context(Configuration));
        }
    
        public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {
            app.UseIdentity();
    
            app.UseMvc(routes =>
            {
                routes.MapRoute(
                      name: "default",
                      template: "{controller}/{action}/{id?}",
                      defaults: new { controller = "Home", action = "Index" });
            });
        }
    }
    

    DbContext

    public sealed class Context : IdentityDbContext
    {
        private readonly IConfiguration _config;
        public DbSet Clients { get; set; }
    
        public Context(IConfiguration config)
        {
            // store the injected config
            _config = config;
        }
    
        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            modelBuilder.ForSqlServer().UseIdentity();
            base.OnModelCreating(modelBuilder);
        }
    
        protected override void OnConfiguring(EntityOptionsBuilder optionsBuilder)
        {
            // use the injected config
            optionsBuilder.UseSqlServer(_config.Get("Data:DefaultConnection:ConnectionString"));
    
            base.OnConfiguring(optionsBuilder);
        }
    }
    

    Controller

    [Route("api/[controller]")]
    public class TestController : BaseController
    {
        private readonly Context _context;
    
        // have the context injected
        public TestController(Context context)
        {
            _context = context;
        }
    
        [HttpGet]
        public ActionResult Get()
        {
            return new ObjectResult(_context.Clients.ToList());
        }
    }
    

提交回复
热议问题