Adding A Configuration Source File In ASP.NET Core 1.0

Introduction  
 
In this article, I will explain how to add a configuration source in ASP.NET Core 1.0. This is the simplest way to access the JSON file information in ASP.NET Core 1.0.
 
Before reading this article, you must read the articles given below for ASP.NET Core knowledge.
Package required
 
We need to add the following JSON package in package.json file. This package will help to access the information in ASP.NET Core 1.0.
  1. "Microsoft.Extensions.Configuration.Json""1.0.0"  
Creating a json file 
 
We are creating a configuration JSON file in our application.
 
Right click -> Add -> New Item.. -> Click "Code" inside the Installed Category -> Select json file. 
 
 
 
appsettings.json
 
We have added one sample message inside the JSON file.
  1. {  
  2.   "Message": { "WelcomeMessage""Configure Source In ASP.NET Core 1.0 !!" },  
  3.   "Microsoft": {  
  4.     "Platform": [ "My""New""Article !! " ]  
  5.   }  
  6. }  
JSON configuration
 
In the following code, we used "AddJsonFile("json file name")" extension method. We can access the JSON file in our application through this extension "AddJsonFile("appsettings.json")".  
  1. using Microsoft.AspNetCore.Builder;  
  2. using Microsoft.AspNetCore.Hosting;  
  3. using Microsoft.AspNetCore.Http;  
  4. using Microsoft.Extensions.Configuration;  
  5. using Microsoft.Extensions.DependencyInjection;  
  6. using Microsoft.Extensions.Logging;  
  7. using System.Collections.Generic;  
  8. using System.Linq;  
  9.    
  10. namespace ConfiguringSource  
  11. {  
  12.     public class Startup  
  13.     {  
  14.         public Startup(IHostingEnvironment env)  
  15.         {  
  16.             var ConfigBuilder = new ConfigurationBuilder().SetBasePath(env.ContentRootPath)  
  17.                                                           .AddJsonFile("appsettings.json");  
  18.             Configuration = ConfigBuilder.Build();  
  19.         }  
  20.         // This method gets called by the runtime. Use this method to add services to the container.  
  21.         // For more information on how to configure your application, visit http://go.microsoft.com/fwlink/?LinkID=398940  
  22.         public IConfiguration Configuration { getset; }  
  23.         public void ConfigureServices(IServiceCollection services)  
  24.         {  
  25.    
  26.         }  
  27.    
  28.         // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.  
  29.         public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)  
  30.         {  
  31.             loggerFactory.AddConsole();  
  32.    
  33.             if (env.IsDevelopment())  
  34.             {  
  35.                 app.UseDeveloperExceptionPage();  
  36.             }  
  37.    
  38.             app.Run(async (context) =>  
  39.             {  
  40.                 var Message = Configuration["Message:WelcomeMessage"];  
  41.                 var Details = Configuration.GetSection("Microsoft:Platform").GetChildren().Select(x => x.Value);//array of value  
  42.                 await context.Response.WriteAsync(string.Join(" ", Details) + Message);  
  43.                    
  44.             });  
  45.         }  
  46.     }  
  47. }  
IConfiguration Interface
 
IConfiguration Interface represents a set of key/value application configuration properties.
 
Possible Error !! 
 
We have added all packages and libraries but we are getting the “File Not Found" Exception. This is not a big deal! We can resolve this Internal Server Error in two ways. 
 
Fixing Error 1
 
We need to specify the exact path of "appsettings.json" file. The following code includes "IHostingEnvironment" to find the path of the JSON file or any other file inside the application.
  1. public Startup(IHostingEnvironment env)  
  2. {  
  3.     var builder = new ConfigurationBuilder()  
  4.         .SetBasePath(env.ContentRootPath)  
  5.         .AddJsonFile("appsettings.json");  
  6.    
  7.     Configuration = builder.Build();  
  8. }  
Fixing Error 2
 
This is a common answer. We can directly put "Directory.GetCurrentDirectory()" to find the path of the file. 
  1. public Startup()  
  2. {  
  3. var builder = new ConfigurationBuilder()  
  4. .SetBasePath(Directory.GetCurrentDirectory())  
  5. .AddJsonFile("appsettings.json");  
  6.    
  7. Configuration = builder.Build();  
  8. }  
project.json
 
The final structure of project.json file in our application is given below. 
  1. {  
  2.   "dependencies": {  
  3.     "Microsoft.NETCore.App": {  
  4.       "version""1.0.1",  
  5.       "type""platform"  
  6.     },  
  7.     "Microsoft.AspNetCore.Diagnostics""1.0.0",  
  8.     "Microsoft.AspNetCore.Server.IISIntegration""1.0.0",  
  9.     "Microsoft.AspNetCore.Server.Kestrel""1.0.1",  
  10.     "Microsoft.Extensions.Logging.Console""1.0.0",  
  11.     "Microsoft.Extensions.Configuration.Json""1.0.0"  
  12.   },  
  13.    
  14.   "tools": {  
  15.     "Microsoft.AspNetCore.Server.IISIntegration.Tools""1.0.0-preview2-final"  
  16.   },  
  17.    
  18.   "frameworks": {  
  19.     "netcoreapp1.0": {  
  20.       "imports": [  
  21.         "dotnet5.6",  
  22.         "portable-net45+win8"  
  23.       ]  
  24.     }  
  25.   },  
  26.    
  27.   "buildOptions": {  
  28.     "emitEntryPoint"true,  
  29.     "preserveCompilationContext"true  
  30.   },  
  31.    
  32.   "runtimeOptions": {  
  33.     "configProperties": {  
  34.       "System.GC.Server"true  
  35.     }  
  36.   },  
  37.    
  38.   "publishOptions": {  
  39.     "include": [  
  40.       "wwwroot",  
  41.       "web.config"  
  42.     ]  
  43.   },  
  44.    
  45.   "scripts": {  
  46.     "postpublish": [ "dotnet publish-iis --publish-folder %publish:OutputPath% --framework %publish:FullTargetFramework%" ]  
  47.   }  
  48. }  
Array

The following code will read the array of value in JSON file.
  1. app.Run(async (context) =>  
  2.             {  
  3.                 var Message = Configuration["Message:WelcomeMessage"];  
  4.                 var Details = Configuration.GetSection("Microsoft:Platform").GetChildren().Select(x => x.Value);//array of value  
  5.                 await context.Response.WriteAsync(string.Join(" ", Details) + Message);  
  6.                    
  7.             });  
Output
 

Reference 
Conclusion
 
We learned about adding a configuration source in ASP.NET Core 1.0 and I hope you liked this article. Please share your valuable suggestions and feedback.