When ASP.NET was introduced, as opposed to the standard web.config, it was project.json. When I started checking the ASP.NET code, I was also unable to locate the System.Configuration namespace. This message applies to reading configuration in ASP.NET from project.json data. The ASP.NET orchestration system has been rebuilt from previous versions of ASP.NET. A brand new version of the configuration provides streamlined access to key / value based settings that can be obtained from a variety of sources.
Here is a suggested strategy for taking care of orchestration in ASP.NET 5
- As a basic step you have to consist of the recommendation “Microsoft.Framework.ConfigurationModel” in project.json under dependencies.
- Create an instance of Configuration in your app’s Startup class. – An Arrangement class is simply a collection of Sources that provide the ability to check and save name / value pairs. After you instantiate a configuration class, you must evaluate at least one orchestration source on the configuration instance. You can use various configuration resources such as JSON, XML, INI and so on.
- Use the Options pattern to access specific settings. – Alternatives is a framework for accessing and configuring POCO settings. You will certainly create a class that you can map to your configuration, and also inject it as a dependency into your controller using the ASP.NET 5 dependency shot feature.
So here is the execution. In Startup.cs I have a house arrangement, and in the installer I create circumstances and assign JSON data. You call a reference to Microsoft.Framework.ConfigurationModel.
public Startup(IHostingEnvironment env) { var configuration = new Configuration(); configuration.AddJsonFile("config.json"); Configuration = configuration; } public IConfiguration Configuration { get; set; } public void ConfigureServices(IServiceCollection services) { services.AddMvc(); services.Configure<AppSettings>(Configuration.GetSubKey("AppSettings")); }
Then I read the AppSettings configuration section. And in a controller class, you need to create a producer that accepts an IOptions argument.
private readonly AppSettings _appSettings; public HomeController(IOptions<AppSettings> settingsAccessor) { _appSettings = settingsAccessor.Options; }
The ASP.NET runtime will introduce the settings into the controller builder, this is where you can start consuming the configuration values.
And below is the AppSettings class.
public class AppSettings { public string SiteTitle { get; set; } public string InstrumentationKey { get; set; } }
And also below is the config.json file.
{ "AppSettings": { "SiteTitle": "AppInsightsDemo", "InstrumentationKey": "db6da75a-s787-4a7b-gh88-b0041b8a9299" } }