(C# ASP.NET Core) Storing and Reading Configuration Key Value Data

We have seen in the previous tutorials that appSettings json file can be used to configure log levels. But this is not the only use of this file - it can, as well, be used to store custom key value data that can be read anywhere in your application. It turns out that log levels are just one of the possible values - there is a lot more that can be done. In this tutorial we shall learn how to store some rudimentary data and then read it back on a razor page.
(Rev. 19-Mar-2024)

Categories | About |     |  

Parveen,

Create any Hello Razor App

Open visual studio and create any simple, hello world type of asp.net core application.

We have created an application of one razor page called index.

Let us ensure that the app will run under development environment, so, open the program.cs file and set the environment to development.

// set the envirnment to development 
var builder = WebApplication.CreateBuilder(new WebApplicationOptions()
{

  EnvironmentName = Environments.Development
}

);

builder.Services.AddRazorPages();

var app = builder.Build();

app.MapRazorPages();

app.Run();

Locate the appSettings.Development json file and open it. If you do not find this file, then you can create it, and write any json properties similar to what you see here.


  {
  "Company": "XYZ Company",

  "Address": {

    "City": "my city",

    "ISD Code": 34
  },

  "DetailedErrors": true,

  "Logging": {

    "LogLevel": {

      "Default": "Warning",

      "Microsoft.AspNetCore": "Error"

    }
  }
}

  

We have added two json properties. One is a simple property called Company, and the other is a complex property called Address.

Video Explanation

Please watch the following youtube video:

How to programmatically read the configuration

Let us now read the properties programmatically and display their values on the index home page.

Open the backing file index.cshtml.cs

using Microsoft.AspNetCore.Mvc.RazorPages;

namespace MyRazorApp.Pages
{

  public class IndexModel : PageModel
  {

    private readonly IConfiguration _cfg;


    // dependency through ctor 
    public IndexModel(IConfiguration cfg)
    {

      _cfg = cfg;

    }

    public string? TextMessage { get; set; }

    // on get method 
    public void OnGet()
    {

      String CompanyName = _cfg.GetValue<String>("Company");

      // notice the syntax for reading 
      // a nested property 
      String CityName = _cfg ["Address:City"];

      // read an int 
      Int32 ISDCode = _cfg.GetValue<Int32>("Address:ISDCode");

      TextMessage =
           $"Company is {CompanyName}, " +
           $"City is {CityName} and " +
           $"ISD {ISDCode} ";

    }

  }

}

The first step is to use constructor dependency injection and obtain the interface IConfiguration. It can be cached as a readonly member.

We have also added a string property called TextMessage to hold the values as a single string.

The property Company has been read by using the GetValue method.

We could have equivalently used the brackets indexer, as we have done for reading the nested City property.

Notice the syntax for reading a nested property.

ISDCode is another property of integer type. We have used the GetValue function for extracting its value as an integer.

Now let us open the index cshtml razor page for displaying the string.

  
@page

@model MyRazorApp.Pages.IndexModel

<p>
  @Model.TextMessage
</p>
  
  

As you can see, the string is shown in a paragraph (p) tag.

Run the Application

Let us run the application. Allow the page to open.

We can verify that the appSettings file has been read and the values are displayed as expected (also see the linked video).


This Blog Post/Article "(C# ASP.NET Core) Storing and Reading Configuration Key Value Data" by Parveen is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.