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.
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.
We are creating a configuration JSON file in our application.
Right click -> Add -> New Item.. -> Click “Code” inside the Installed Category -> Select json file.
We have added one sample message inside the JSON file.
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”)”.
IConfiguration Interface represents a set of key/value application configuration properties.
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.
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.
This is a common answer. We can directly put “Directory.GetCurrentDirectory()” to find the path of the file.
The final structure of project.json file in our application is given below.
The following code will read the array of value in JSON file.
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.