$mkdir appsettings $cd appsettings $dotnet new $dotnet restoreNow create appsettings.json file:
{ "AppSettings": { "Date": "1900-01-01" } }Date is the setting I am interested in loading from AppSettings section.
Edit project.json file to include "Microsoft.Extensions.Configuration.UserSecrets": "1.1.0" in dependencies section. This is the only extra library we need in order to use ConfigurationBuilder functions.
{ "version": "1.0.0-*", "buildOptions": { "debugType": "portable", "emitEntryPoint": true, "outputName": "AppSettings" }, "dependencies": {}, "frameworks": { "netcoreapp1.0": { "dependencies": { "Microsoft.NETCore.App": { "type": "platform", "version": "1.1.0" }, "Microsoft.Extensions.Configuration.UserSecrets": "1.1.0" }, "imports": "dnxcore50" } } }Now lets edit the Program.cs file. We are going to load the Date setting and display it in Console. Don't forget to include "using Microsoft.Extensions.Configuration;"
using System; using System.IO; using Microsoft.Extensions.Configuration; namespace ConsoleApplication { public class Program { public static string AppSettingsFile = "appsettings.json"; public static string AppSettingsSection = "AppSettings"; public static string DateSetting = "Date"; public static void Main(string[] args) { var builder = new ConfigurationBuilder() .SetBasePath(Directory.GetCurrentDirectory()) .AddJsonFile(Program.AppSettingsFile, optional: true, reloadOnChange: true); var config = builder.Build(); var appSettings = config.GetSection(Program.AppSettingsSection); string dateString = appSettings[Program.DateSetting]; Console.WriteLine("Date:" + dateString); } } }That is pretty much all. Now let's build and run it.
$dotnet build $dotnet run Date:1900-01-01
Nice article I was really impressed by seeing this article, it was very interesting and it is very useful for Learners. I get a lot of great information from this blog. Thank you for your sharing this informative blog
ReplyDeleteSelenium Training in Chennai
Selenium Course in Chennai
Hai Author Good Information that i found here,do not stop sharing and Please keep updating us..... Thanks.
ReplyDeletehire asp.net developer
.net development services
It is very good and useful for students and developer .Learned a lot of new things from your post!Good creation ,thanks for good info .Net Online Training Hyderabad
ReplyDelete