(C# ASP.NET Core) Using Options Pattern to Read Configuration

In the previous tutorial we read various properties of an appSettings configuration file. But there is a better method for reading complex properties - such as "Address" - that we read in the previous tutorial. This is done through the options pattern. Let us now read City and ISDCode of the Address property by using the options pattern.
(Rev. 19-Mar-2024)

Categories | About |     |  

Parveen,

A brief on the options pattern

Options pattern uses a class to map a complex property - i.e., a nested property.

Consider the Address property in the appSettings file that we were discussing in the previous tutorial. This property can be mapped to a class like the one that you are seeing here:

public class AddressOptions
{

  // fields are not bound 
  // this field stores the name of 
  // the complex property Address 
  // it is also called a section 
  public const string Address = "Address";


  // only public properties bind 
  public String? City { get; set; }

  // an int property 
  public Int32 ISDCode { get; set; }
}

This class is called an options class. It is usually suffixed with Options for readability - so we have named it AddressOptions. It contains two public properties corresponding to the two properties of the Address section. Notice that we have added a constant string to hold the name of the Address section so that we do not have to type it again and again.

Some characteristics of an options class are:

  1. An options class should not be an abstract class.
  2. It should have a default constructor.
  3. Only the public properties of this class are mapped.
  4. Other data members and fields are not mapped - are left un-touched.

This class can now be bound to the Address section of the json file. Let us do it practically!

Video Explanation

Please watch the following youtube video:

Add Options Class

Open the project in visual studio. This is the appsettings file that we were working in our previous tutorial. This file has a section called Address. We have to read this section and display the properties on a razor page.

Add a folder called Options and then add a class called AddressOptions to this folder. Let us have a look at the class (see the code shown above). This class has two properties that correspond to those of the Address section of the appSettings json file.

Open the backing class of the index razor page. We have discussed this class in the previous tutorial. So now we shall straightway come to the OnGet method.

// refer the previous tutorial 
// for details of the Index.cshtml.cs file 
public void OnGet()
{

  var addressOptions = new AddressOptions();

  _cfg
      .GetSection(AddressOptions.Address)
      .Bind(addressOptions);

  TextMessage =
        $"City is {addressOptions.City} and " +
        $"ISD {addressOptions.ISDCode} ";

}

First create an instance of the options class.

The GetSection method of IConfiguration helps us reach the Address section of the json file.

The Bind method extracts the properties from the json file and sets them on our options instance.

The City and ISDCode are formatted into the TextMessage string.

Let us open the Index page. Here we display the ISDCode and City contained in the TextMessage string. It is the same file as we had in the previous tutorial. So please refer the previous tutorial for that part.


@page

@model MyRazorApp.Pages.IndexModel

<p>

  @Model.TextMessage

</p>

We can now run the project to verify that the Address section has been correctly read. Thanks!


This Blog Post/Article "(C# ASP.NET Core) Using Options Pattern to Read Configuration" by Parveen is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.