Simplest way to use strongly typed configuration in dotnet core
Tags: beginners, dotnet
Published: 2021-01-15 21:06:22

Hello Friends,

Every project  has some kind of configuration file, in the format of XML, json etc. Dotnet core projects use Json based config file in which we can store project level settings in Json format. There are mutliple ways to access these values across the project as shown in this  [official documentation](https://docs.microsoft.com/en-us/aspnet/core/fundamentals/configuration/?view=aspnetcore-5.0) .

My favorite one is Strongly Typed because ..
* Its less prone to string comparison errors.
* Provides intellisense for easier access.
* and of-course, type safe.

Below is the easiest way I know to use strongly typed approach in asp.net core application.

Lets start by creating a basic asp.net API application.
Detailed steps can be found  [here](https://docs.microsoft.com/en-us/aspnet/core/tutorials/first-web-api?view=aspnetcore-5.0&tabs=visual-studio) 

This should put `appsettings.json` file in the root project.


> ASP.Net core's configuration can automatically detect the presence of `appsettings.json` file if its with this name

I have added custom section "**AppDetails**" in my appsettings.json file.

```
{
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft": "Warning",
      "Microsoft.Hosting.Lifetime": "Information"
    }
  },
  "AppDetails": {
    "AppName": "MyCoolApp",
    "AppVersion": "V1"
  },
  "AllowedHosts": "*"
}
``` 

Now create a class called `AppDetails.cs` to hold the values from this section.

```
  public class AppDetails
    {
        public string AppName { get; set; }
        public string AppVersion { get; set; }
    }
```


> In the class file, make sure the field names match with those in the appsettings.json file.


Now open `Startup.cs` class and add below line `ConfigureServices` method

```
services.Configure<AppDetails>(Configuration.GetSection("AppDetails"));
```

In this code, we configuring AppDetails class to hold data from Appdetails section from the config file.

 
>Again, point to note here is, ASP.Net core's configuration service can automatically detect the presence of `appsettings.json` file if its with this name. Hence it knows where to look for the section with the name "**AppDetails**".

And that's basically it. Now we can access these settings anywhere in our application using Dependency Injection

Now , in your controller, create a property for AppDetails class , inject  `IOptions<AppDetails>` as a dependency and assign it to the instance.

```
    [ApiController]
    [Route("[controller]")]
    public class CoolController:Controller
    {
        AppDetails configs { get; set; }
        public CoolController( IOptions<AppDetails> Configuration )
        {
            configs = Configuration.Value;
        }
        public string GetVersion( )
        {
            return configs.AppName + ":" + configs.AppVersion;
        }
    }
```
If you run the application and launch `https://localhost:44350/cool`, you should see it return the values from our appsettings.json file.

![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1610744054536/z60mvAID9.png)


Please do share if you find it helpful or otherwise.