ASP.NET Core MVC Interview Question #132
How should configuration be consumed in MVC services?
Dependency Injection, Services, Error Handling, Logging & Performance Senior Advanced
Quick Interview Answer
ASP.NET Core configuration can be accessed directly through IConfiguration, but strongly typed options are usually clearer for related settings. The options pattern binds configuration sections to classes and supports validation and different consumption models.
Detailed Explanation
ASP.NET Core configuration can be accessed directly through IConfiguration, but strongly typed options are usually clearer for related settings. The options pattern binds configuration sections to classes and supports validation and different consumption models.
Application secrets such as passwords or API keys should not be hard-coded in controllers, source files, or committed configuration. Use appropriate secret-management mechanisms for the deployment environment.
Code Example
builder.Services.Configure<EmailOptions>(
builder.Configuration.GetSection("Email"));
public class EmailService
{
public EmailService(
IOptions<EmailOptions> options)
{
var settings = options.Value;
}
}