Introduction
In most of our applications, we need some kind of configuration. When we have a fixed value and we know we might need to change it in future (i.e. connection string), we usually add it to our configuration file.
In the case of ASP.NET MVC, we usually add it to the web.config file. But with a .NET Core project, we will have to add it to the appsettings.json file. Here is an example of ASP.Net MVC config and .NET Core config.
ASP.Net MVC
- <configuration>
- <appSettings>
- <add key="apiUrl" value="http://localhost:2020"/>
- </appSettings>
- </configuration>
.NET Core
- {
- "Logging": {
- "LogLevel": {
- "Default": "Information",
- "Microsoft": "Warning",
- "Microsoft.Hosting.Lifetime": "Information"
- }
- },
- "apiUrl": "http://localhost:2020"
- }
Here is the code to read config values in the server file: (MVC controller / BLL / .NET Core controller).
ASP.NET MVC
- var apiUrl = ConfigurationManager.AppSettings["apiUrl"];
.NET core
- public class HomeController : Controller
- {
- public IConfiguration Config { get; set; }
- public HomeController( IConfiguration config)
- {
- Config = config;
- }
- public IActionResult Index()
- {
- string apiConfig = this.Config["apiConfig"];
-
- return View();
- }
- }
Output
As you can see, we can read our config value by simply defining the key in the config service.
Similarly, when we wish to store multiple values in config, how can we achieve it? Lets understand when do we need such scenario. Suppose we have a city dropdown in a online delivery site. This site works only for few cities as of now but in future it might operate in one or two more cities. Then how can we configure it?
Here we can use two approaches to solve the problem:
- We can define a configuration page in the application.
- We can define all cities as an array type in config.
Let's discuss how to achieve it using the second approach. First, we should define all cities that we wish to display in our application. Here is the piece of code that we need to add to appsettings.json.
- {
- "Logging": {
- "LogLevel": {
- "Default": "Information",
- "Microsoft": "Warning",
- "Microsoft.Hosting.Lifetime": "Information"
- }
- },
- "Cities": [ "City 1", "City 2", "City 3" ],
- "apiUrl": "http://localhost:2020"
- }
Let's try to use our previous approach to read the config value.
Please ignore extra code. That's for my reference.
As you can see, the value is null here. So, when we have value as array type, we need to add extra lines of code to read it. Here is the code.
- public IActionResult Index()
- {
- List<string> cities = this.Config.GetSection("Cities")?.GetChildren()?.Select(x => x.Value)?.ToList();
-
- return View();
- }
Here, we are trying to find the "Cities" section in the application config (appsettings.json), and then GetChildren() method is used to read all children.
After that, we are trying to loop through each item and find its values by using the Select() extension method. Finally, use ToList() to convert our data list format so that we can directly bind it with the City dropdown.
Here is the output:
As you can see we have all cities that are configured in appsettings.json in an action. Hope this will help you someday. Thanks for reading it.
Please feel free to post any suggestions or feedback.