reading arrays of objects from appsettings.json

team upsilon 36 Reputation points
2022-07-28T10:52:56.233+00:00
{  
  "Logging": {  
    "LogLevel": {  
      "Default": "Information",  
      "Microsoft.AspNetCore": "Warning"  
    }  
  },  
  "AllowedHosts": "*",  
  "TimeInterval": [  
     {  
      "Title": "6 hours",  
      "Hours": 6  
    },  
    {  
      "Title": "1 day",  
      "Hours": 24  
    }  
  ]  
}  

Above is what i have in my appsettings.json. I am able to read single properties of objects however i cant read the whole array at once. Preferably i'd like to parse it into a TimeInterval object too. How can I do this? I am using blazor server side.

Developer technologies | .NET | Blazor
Developer technologies | C#
0 comments No comments
{count} votes

Accepted answer
  1. Reza Aghaei 4,986 Reputation points MVP Volunteer Moderator
    2022-07-28T19:38:56.047+00:00

    You should use Dependency Injection and the .NET Configuration system:

    1. Inject IConfiguration, and store the instance in a member/variable like Configuration
    2. Add the using Microsoft.Extensions.Configuration statement
    3. Read the app settings like this: var intervals = Configuration.GetSection("TimeInterval").Get<List<TimeInterval>>();

    You can also use options pattern to get the settings. But to avoid a long post, I'll just share two examples on how to get configs in the page or in a page model. Fore more information, take a look at links at the bottom of the post.

    Example 1 - Getting configuration in a Blazor Page

    Assuming you have the following class:

    public class TimeInterval  
    {  
        public string Title { get; set; }  
        public int Hours { get; set; }  
    }  
    

    Then in a page, you can inject IConfiguration and read the settings:

    @page "/"  
    @using Microsoft.Extensions.Configuration  
    @inject IConfiguration Configuration  
    <h1>Hello, world!</h1>  
    <ul>  
    @{  
        var intervals = Configuration.GetSection("TimeInterval").Get<List<TimeInterval>>();  
        foreach (var item in intervals)  
        {  
            <li>@item.Title: @item.Hours Hours</li>  
        }  
    }  
    </ul>  
    Welcome to your new app.  
    

    Example 2 - Getting configuration in a PageModel class:

    Inject the IConfiguration and read settings like this:

    using Microsoft.AspNetCore.Mvc.RazorPages;  
    using Microsoft.Extensions.Configuration;  
    using System.Collections.Generic;  
      
    namespace WebApplication1.Pages  
    {  
        public class IndexModel : PageModel  
        {  
            public IConfiguration Configuration;  
            public IndexModel(IConfiguration configuration)  
            {  
                Configuration = configuration;  
            }  
            public void OnGet()  
            {  
                var intervals = Configuration.GetSection("TimeInterval").Get<List<TimeInterval>>();  
            }  
        }  
    }  
    

    More information:

    4 people found this answer helpful.

1 additional answer

Sort by: Most helpful
  1. Karen Payne MVP 35,586 Reputation points Volunteer Moderator
    2022-07-28T13:52:40.043+00:00

    One option using System.Text.Json

    public class RootSettings  
    {  
        public Timeinterval[] TimeInterval { get; set; }  
    }  
    

    Read

    Timeinterval[] result =  
        JsonSerializer.Deserialize<RootSettings>(  
            File.ReadAllText("appsettings.json")).TimeInterval;  
    
    1 person found this answer helpful.

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.