How to Avoid Hardcoding Application Settings
Have values that change between environments?
Put them in the configuration.
Instead of:
var apiUrl = "https://prod-api.example.com";
var timeout = 30;Read them from configuration:
{
"ExternalApi": {
"Url": "https://api.example.com",
"TimeoutSeconds": 30
}
}Then bind them to options:
builder.Services.Configure<ExternalApiOptions>(
builder.Configuration.GetSection("ExternalApi"));
Configuration works well for:
- URLs
- Timeouts
- Feature settings
- Connection strings
- Environment-specific values
Secrets should usually live in a dedicated secret store rather than source code.
If a value changes without requiring a code change, configuration is usually a better home for it.