Skip to content

Instantly share code, notes, and snippets.

@manoj-choudhari-git
Created April 20, 2021 20:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save manoj-choudhari-git/7b77cb7c11572dc070641b0c06cb8301 to your computer and use it in GitHub Desktop.
Save manoj-choudhari-git/7b77cb7c11572dc070641b0c06cb8301 to your computer and use it in GitHub Desktop.
Using Configure method to register the options
// Startup.cs
public class Startup
{
public void ConfigureServices(IServiceCollection services)
{
// Some code...
services.Configure<MailFeature>(Configuration.GetSection(nameof(MailFeature)));
// Some other code....
}
}
// Inject dependency in controller.
// using Microsoft.Extensions.Options;
public class HomeController : Controller
{
private readonly ILogger<HomeController> _logger;
private readonly IOptions<MailFeature> mailFeatureOptions;
// the Options class is directly injected
public HomeController(ILogger<HomeController> logger, IOptions<MailFeature> mailFeatureOptions)
{
_logger = logger;
this.mailFeatureOptions = mailFeatureOptions;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment