Successfully added
ASP.NET Core
by Marlon
Config Injection & Options Pattern in Razor Page
Inject Configuration
To inject configuration settings in your ASP.NET Core application, use Microsoft.Extensions.Configuration
and inject IConfiguration
into your page or component. Access configuration values using Configuration["KeyName"]
.
@using Microsoft.Extensions.Configuration
@inject IConfiguration Configuration
<!-- Access configuration value -->
@Configuration["ApplicationInsights:InstrumentationKey"]
Options Pattern
For more structured configuration management, use the options pattern with Microsoft.Extensions.Options
. Inject IOptionsMonitor<TOptions>
with @inject
and access configuration values through OptionsAccessor.Value.PropertyName
.
@using Microsoft.Extensions.Options
@inject IOptionsMonitor<ApplicationInsightsOptions> ApplicationInsightsOptionsAccessor
<!-- Access configuration value -->
var instrumentationKey = ApplicationInsightsOptionsAccessor.Value.InstrumentationKey;
For detailed information, refer to the ASP.NET Core documentation on configuration options.
Referenced in:
Comments