June 10 2024
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*",
"NewsletterSettings": {
"URL": "testUrl"
}
}
public class NewsletterSettings
{
public string Url { get; set; }
}
builder.Services.Configure<NewsletterSettings>(
builder.Configuration.GetSection(nameof(NewsletterSettings)));
[ApiController]
[Route("[controller]")]
public class NewsletterController : ControllerBase
{
private readonly IOptions<NewsletterSettings> _newsletterSettings;
public NewsletterController(IOptions<NewsletterSettings> newsletterSettings)
{
_newsletterSettings = newsletterSettings;
}
[HttpGet(Name = "GetNewsletter")]
public string Get()
{
return _newsletterSettings.Value.Url;
}
}
[ApiController]
[Route("[controller]")]
public class NewsletterController : ControllerBase
{
private readonly IOptionsMonitor<NewsletterSettings> _newsletterMonitor;
public NewsletterController(IOptionsMonitor<NewsletterSettings> newsletterMonitor)
{
_newsletterMonitor = newsletterMonitor;
}
[HttpGet(Name = "GetNewsletter")]
public string Get()
{
string ioptionsMonitor = _newsletterMonitor.CurrentValue.Url;
return ioptionsMonitor;
}
}
public NewsletterController(IOptionsMonitor<NewsletterSettings> newsletterMonitor)
{
_newsletterMonitor = newsletterMonitor;
_newsletterMonitor.OnChange(settings =>
{
Console.WriteLine($"Settings changed: {settings.Url}");
});
}
Join 13,250+ subscribers to improve your .NET Knowledge.
Go-to resource for understanding the core concepts of design patterns without the overwhelming complexity. In this concise and affordable ebook, I've distilled the essence of design patterns into an easy-to-digest format. It is a Beginner level. Check out it here.
Every Monday morning, I share 1 actionable tip on C#, .NET & Arcitecture topic, that you can use right away.
Subscribe to the TheCodeMan.net and be among the 13,250+ subscribers gaining practical tips and resources to enhance your .NET expertise.