How to add ASP.NET Core Web configuration file

By | February 20, 2017

This article will help you to understand about adding the ASP.NET Core Web Configuration File. In my previous article I have discussed about the prerequisites to develop ASP.NET core web project and the asp.net core web project structure. The links are below.

Become an Expert in Programming and Web Development.View all courses!

Adding A ASP.NET Core Web Configuration File

  • The web.config is not the actual configuration file.
  • The only job of the web.config file is to tell the IIS to route the request to aspNetCore handler.
  • We can provide the json configuration file to provide the run time settings to the application.
  • Right click on the web project. Click  Add > New Item. Select ASP.NET Configuration File from ASP.NET and add it to the project. Name the file as appsettings.json

    ASP.NET Core Web Configuration File

    ASP.NET Core web Configuration File

  • Change the json as shown in the figure below.
  • We need to add the below code to add the configuration reference to the project. I have to change the project.json file

    Please check the below figure.

  • Once the above line is added, the project contact the nuget server and download the reference for the project.
  • When ASP.NET calls the startup class it provides an variable which denotes the folder path. That is why there is no need to go for any hard coding.To read the configuration file I need to add a constructor to the StartUp.cs

 

Please check the code for the Startup.cs in below figure.

Once we change the Startup.cs constructor and add the code as shown in the snippet above, I have to add new nuget references by pressing the “ctrl + .(dot)” on the error code as shown in the below figure.

and we can see that one more dependency is added to the project.json as  “Microsoft.Extensions.Configuration.Json”: “1.0.0”

 

Now as we have the setup ready to read the configuration file. We can see in the above code that we are reading the “HelloWorld” message in the Configure method and writing it on the response.

Once we run the project we will see the message “Hello World from Configuration” in the browser.

Conclusion

We have added the ASP.NET Core Web Configuration File to the exiting project. And see how it is  different from the web.config file we have used till now in our web development.