Building on @Ogglas answer. I changed the service Configuration.Bind to bind a section of an appSettings.json.
appSettings.json
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*",
"myappSettings": {
"Company": "ACME Sandbox",
"ApiEndpoint": "https://api.sample.com:50443/",
"ImageLocation": "\\\\OnPremise.sample.local\\Images",
"ImageUrl": "https://cdn.somesite.com",
"Emails": {
"RequestTo": "team@degensrt.com",
"SubmittedTo": "team@degensrt.com",
}
}
}
myappSettings.cs
public class myappSettings
{
public string Company { get; set; }
public string ApiEndpoint { get; set; }
public string ImageLocation { get; set; }
public string ImageUrl { get; set; }
public appEmails Emails { get; set; }
}
public class appEmails
{
public string RequestTo { get; set; }
public string SubmittedTo { get; set; }
}
Then register and bind to the section myappSettings: in Program.cs:
//Application Settings
var settings = new myappSettings();
builder.Configuration.Bind("myappSettings", settings);
builder.Services.AddSingleton(settings);
Then inject and consume in your component:
@inject appSettings appSettings;
....
....
@code {
private string EmailTo = "";
protected override async Task OnInitializedAsync()
{
EmailTo = appSettings.Emails.SubmittedTo;
}
}