Created
April 21, 2021 15:50
-
-
Save manoj-choudhari-git/9b41afecddf0bc7b2eb07fe6492486de to your computer and use it in GitHub Desktop.
Code changes for IOptionsMonitor demo
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// MailFeatureOptions.cs | |
public class MailFeatureOptions | |
{ | |
public bool IsEnabled { get; set; } | |
public string SmtpServer { get; set; } | |
public string FromEmailAddress { get; set; } | |
public string Subject { get; set; } | |
} | |
// Startup.cs | |
public class Startup | |
{ | |
public void ConfigureServices(IServiceCollection services) | |
{ | |
// some other code... | |
services.Configure<MailFeatureOptions>(Configuration.GetSection(nameof(MailFeatureOptions))); | |
services.AddControllersWithViews(); | |
} | |
// some other code... | |
} | |
// HomeController.cs | |
public class HomeController : Controller | |
{ | |
private readonly ILogger<HomeController> _logger; | |
private readonly IOptionsMonitor<MailFeatureOptions> mailFeatureOptionsMonitor; | |
public HomeController(ILogger<HomeController> logger, IOptionsMonitor<MailFeatureOptions> mailFeatureOptionsMonitor) | |
{ | |
_logger = logger; | |
this.mailFeatureOptionsMonitor = mailFeatureOptionsMonitor; | |
} | |
public IActionResult Index() | |
{ | |
// Load in ViewData | |
ViewData["IsEnabled"] = this.mailFeatureOptionsMonitor.CurrentValue.IsEnabled; | |
ViewData["SmtpServer"] = this.mailFeatureOptionsMonitor.CurrentValue.SmtpServer; | |
ViewData["FromEmailAddress"] = this.mailFeatureOptionsMonitor.CurrentValue.FromEmailAddress; | |
ViewData["Subject"] = this.mailFeatureOptionsMonitor.CurrentValue.Subject; | |
return View(); | |
} | |
} | |
// Views/Home/Index.cshtml | |
<div class="card" style="width: 58rem;"> | |
<div class="card-header"> | |
MailFeatureOptions | |
</div> | |
<div class="card-body"> | |
<ul class="list-group list-group-flush"> | |
<li class="list-group-item text-left"><strong>IsEnabled:</strong> @ViewData["IsEnabled"]</li> | |
<li class="list-group-item text-left"><strong>SmtpServer:</strong> @ViewData["SmtpServer"]</li> | |
<li class="list-group-item text-left"><strong>FromEmailAddress:</strong> @ViewData["FromEmailAddress"]</li> | |
<li class="list-group-item text-left"><strong>Subject: </strong>@ViewData["Subject"]</li> | |
</ul> | |
</div> | |
</div> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment