How can we store configuration data in new asp.net vnext?

后端 未结 3 1858
面向向阳花
面向向阳花 2021-01-05 07:15

How can we store configuration data in new asp.net vnext? web.config still there(system.web removed, so no any web.config,but I like it) or using new json file for configura

相关标签:
3条回答
  • 2021-01-05 07:50

    In the breaking changes (beta 6), Microsoft.Framework.ConfigurationModel assembly name changed to Microsoft.Framework.Configuration This is the modified startup class

     public IConfiguration Configuration { get; set; }
    
    public Startup(IHostingEnvironment env, IApplicationEnvironment appEnv)
    {
         var configurationBuilder = new ConfigurationBuilder(appEnv.ApplicationBasePath).
            .AddJsonFile("config.json")
            .AddEnvironmentVariables();
         Configuration = configurationBuilder.Build();
    }
    
    0 讨论(0)
  • 2021-01-05 07:52

    Configuration used best is a starting point to strongly typed options. So what you want to do in startup is register an Options object, set it up with data you read from configuration, and get it from the DI system in the rest of your code.

    Example:

    Create an Options object

    public class ApplicationOptions
    {
        public bool MyOption { get; set; }
    }
    

    Read the configuration and setup the options object then register it in DI

    public void Configure(IApplicationBuilder app)
    {         
        // rest of setup
    
        var applicationOptions = new ApplicationOptions();
        string myOption;
        if (configuration.TryGet("MyOption", out myOption) &&
                myOption.Equals("True", StringComparison.OrdinalIgnoreCase))
        {
            applicationOptions.MyOption = true;
        }
    
        // For the scenario above also the code below will work.
        // It is currently limited to desktop (ASPNET50, NET45) only.
        // services.Configure<ApplicationOptions>(configuration);
    
        services.AddInstance<ApplicationOptions>(applicationOptions);
    
        // more setup
    }
    

    And resolve it in your code

    public class MyController : Controller
    {
        public IActionResult Index(ApplicationOptions options)
        {
            // do something with the options
        }
    }
    

    Edit: with any practical reusable options object, the code setting it up will probably happen outside the startup class.

    0 讨论(0)
  • 2021-01-05 08:11

    Not sure exactly what kind of data you are trying to store but this work for me. I created this file myconfig.json and store that data that I need in a JSON format.

    This will register your configuration file, I added this code in the startup.cs page.

    Configuration = new Configuration()
    .AddJsonFile("config.json")
    .AddJsonFile("myconfig.json")
    .AddEnvironmentVariables();
    

    To get the data that you need from the your JSON file you need to this at any point on your code.

    IConfiguration Configuration = new Configuration().AddJsonFile("myconfig.json");
    var jsonNodes = Configuration.Get("SomeJsonNode");
    string someJsonString = Configuration.Get("someJsonString");
    

    FYI: At the moment that I tested that code json array were not supported. Not sure if they fixed that now.

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