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.
- ASP.NET core web project prerequisites and first web application
- ASP.NET core project folder structure
Adding an 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
- Change the json as shown in the figure below.
{ "ConnectionStrings": { "DefaultConnection": "Server=(localdb)\\MSSQLLocalDB;Database=_CHANGE_ME;Trusted_Connection=True;MultipleActiveResultSets=true" }, "HelloWorld": "Hello World from Configuration" }
- We need to add the below code to add the configuration reference to the project. I have to change the project.json file
"Microsoft.Extensions.Configuration.FileExtensions": "1.0.0"
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.
public class Startup { public Startup(IHostingEnvironment env) { var confBuilder = new ConfigurationBuilder().SetBasePath(env.ContentRootPath).AddJsonFile("appsettings.json"); Configuraion = confBuilder.Build(); } public IConfiguration Configuraion { get; set; } public void ConfigureServices(IServiceCollection services) { } // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory) { loggerFactory.AddConsole(); if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } app.Run(async (context) => { var messsage = Configuraion["HelloWorld"]; await context.Response.WriteAsync(message); }); } }
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.
Leave a Reply