How to use the appsettings.json file?

49 views Asked by At

I have created a app using Uno Platform, and I need to load some configurations from appsetting.json. I have read the document and add created a class called AppConfig. appsettings.json

{
  "AppConfig": {
    "Environment": "Production",
    "AppCulture": "en",
    "DataPath": "C://",
    "CDN": "Cloudflare"
  },
  "LocalizationConfiguration": {
    "Cultures": [
      "es",
      "zh"
    ]
  }
}

And AppConfig.cs

public record AppConfig
{
    public string? Environment { get; init; }
    public string? AppCulture { get; init; }
    public string? DataPath { get; init; }
    public string? CDN { get; init; }
}

In the launch method I added

.UseConfiguration(configure: configBuilder =>
    configBuilder
        .EmbeddedSource<App>()
        .Section<AppConfig>()
)

Now, for example, I would like to get the settings in a method in class StartUpCheck(it will be called before the app will be shown to load settings), according to the document, I should add a IOptions<AppConfig> parameter to the constructor. However, When I create the class, I don't know what I should pass--there's no variable typed IOptions<AppConfig>. Although I'm using uno platform, it should be the same to other platforms.

I'm an amateurs so this questions may seems to be stupid. I hope that you don't mind it and can give me some advices. Any help is appreciated.

0

There are 0 answers