ConfigurationManager Migration Shim
This sample demonstrates the ConfigurationManager static shim that allows Web Forms code
accessing ConfigurationManager.AppSettings and ConfigurationManager.ConnectionStrings
to work in Blazor without code changes.
1. Reading AppSettings
In Web Forms, you'd read app settings from Web.config. The BWFC shim reads from
IConfiguration instead, using the "AppSettings" section.
Before (Web Forms)
<!-- Web.config -->
<configuration>
<appSettings>
<add key="SiteName" value="My Site" />
<add key="MaxItemsPerPage" value="25" />
</appSettings>
</configuration>
// Code-behind
var siteName = ConfigurationManager.AppSettings["SiteName"];
var maxItems = ConfigurationManager.AppSettings["MaxItemsPerPage"];After (Blazor with BWFC)
// appsettings.json
{
"AppSettings": {
"SiteName": "BlazorWebFormsComponents Demo",
"MaxItemsPerPage": "25"
}
}
// Same code works!
var siteName = ConfigurationManager.AppSettings["SiteName"];
var maxItems = ConfigurationManager.AppSettings["MaxItemsPerPage"];Live Demo - AppSettings
| Key | Value |
|---|---|
SiteName |
BlazorWebFormsComponents Demo |
MaxItemsPerPage |
25 |
EnableFeatureX |
true |
2. Reading ConnectionStrings
Connection strings work the same way — the shim reads from the "ConnectionStrings" section of
IConfiguration.
Before (Web Forms)
<!-- Web.config -->
<connectionStrings>
<add name="DefaultConnection"
connectionString="Server=..." />
</connectionStrings>
// Code-behind
var connStr = ConfigurationManager
.ConnectionStrings["DefaultConnection"]
.ConnectionString;After (Blazor with BWFC)
// appsettings.json
{
"ConnectionStrings": {
"DefaultConnection": "Server=..."
}
}
// Same code works!
var connStr = ConfigurationManager
.ConnectionStrings["DefaultConnection"]
.ConnectionString;Live Demo - ConnectionStrings
| Name | Connection String |
|---|---|
DefaultConnection |
Server=(localdb)\mssqllocaldb;Database=SampleDb;Trusted_Connection=True; |
3. Setup Required
To enable the ConfigurationManager shim, add this line to your Program.cs
after calling UseBlazorWebFormsComponents():
app.UseBlazorWebFormsComponents();
app.UseConfigurationManagerShim(); // Enable ConfigurationManager shim
4. Direct IConfiguration Access (Recommended for New Code)
The ConfigurationManager shim is for migration compatibility. For new Blazor code,
inject IConfiguration directly:
@inject IConfiguration Configuration
@code {
private string siteName = "";
protected override void OnInitialized()
{
siteName = Configuration["AppSettings:SiteName"] ?? "";
}
}
Live Demo - Native IConfiguration
Site Name (via injected IConfiguration): BlazorWebFormsComponents Demo