DEV Community

Rohit Ramname
Rohit Ramname

Posted on

Simplest way to use strongly typed configuration in dotnet core

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 .

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

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": "*"
}
Enter fullscreen mode Exit fullscreen mode

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; }
    }
Enter fullscreen mode Exit fullscreen mode

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"));
Enter fullscreen mode Exit fullscreen mode

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;
        }
    }
Enter fullscreen mode Exit fullscreen mode

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

Please do share if you find it helpful or otherwise.

Top comments (0)