How can I add a custom JSON file into IConfiguration?

后端 未结 2 1327
北海茫月
北海茫月 2020-12-17 16:27

I\'m using asp.net + Autofac.

I\'m trying to load a custom JSON configuration file, and either create/instance an IConfiguration instance based on that, or at least

相关标签:
2条回答
  • 2020-12-17 17:08

    You can do this by using the Options pattern:

    On ASP.NET Core 2, register the config file on Program.cs

    public class Program
    {
        public static void Main(string[] args)
        {
            var configuration = new ConfigurationBuilder()
                .SetBasePath(Directory.GetCurrentDirectory())
                .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
    
                 // custom config file
                .AddJsonFile("myappconfig.json", optional: false, reloadOnChange: false)
                .Build();
    
            BuildWebHost(args, configuration).Run();
        }
    
        public static IWebHost BuildWebHost(string[] args, IConfiguration config) =>
            WebHost.CreateDefaultBuilder(args)
                .UseConfiguration(config)
                .UseStartup<Startup>()
                .Build();
    }
    

    Create a class that matches with your config file:

    public class MyAppConfig
    {
        public string SomeConfig { get; set; }
    
        public int NumberConfig { get; set; }
    }
    

    Register it on ConfigureServices

    public void ConfigureServices(IServiceCollection services)
    {
        services.AddMvc();
    
        services.Configure<MyAppConfig>(Configuration);
    }
    

    Then, just access it in your Controller:

    [Route("api/[controller]")]
    public class ValuesController : Controller
    {
        private readonly MyAppConfig _appConfig;
    
        public ValuesController(IOptions<MyAppConfig> optionsAccessor)
        {
            if (optionsAccessor == null) throw new ArgumentNullException(nameof(optionsAccessor));
            _appConfig = optionsAccessor.Value;
        }
    
        // GET api/values/5
        [HttpGet("{id}")]
        public string Get(int id)
        {
            return _appConfig.SomeConfig;
        }
    }
    

    If you are not in ASP.NET Core 2 yet, the process is almost the same. You just need to add the custom config file on Startup.cs. The rest is basically the same.

    0 讨论(0)
  • 2020-12-17 17:13

    For .Net Core 2.2, you need to modify Program.cs:

    Before

    public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
                WebHost.CreateDefaultBuilder(args)
               .UseStartup<Startup>();
    

    After

    public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
                WebHost.CreateDefaultBuilder(args)
    
                //This is how you attach additional JSON files
                .ConfigureAppConfiguration((hostingContext, config) =>
                {
                    config.AddJsonFile("customSettings.json", optional: false, reloadOnChange: false);
                })
                //End of update
                .UseStartup<Startup>();
    

    For the latest amendments and to add other kinds of custom settings, please refer to Microsoft documentation at the following article.

    0 讨论(0)
提交回复
热议问题