Skip to content

Commit f820e49

Browse files
authored
[Preview 5] Blazor WASM configuration support (#17954)
1 parent 4af697c commit f820e49

1 file changed

Lines changed: 121 additions & 7 deletions

File tree

aspnetcore/blazor/hosting-model-configuration.md

Lines changed: 121 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ uid: blazor/hosting-model-configuration
1111
---
1212
# ASP.NET Core Blazor hosting model configuration
1313

14-
By [Daniel Roth](https://github.com/danroth27)
14+
By [Daniel Roth](https://github.com/danroth27) and [Luke Latham](https://github.com/guardrex)
1515

1616
[!INCLUDE[](~/includes/blazorwasm-preview-notice.md)]
1717

@@ -92,12 +92,21 @@ The `IWebAssemblyHostEnvironment.BaseAddress` property can be used during startu
9292

9393
### Configuration
9494

95-
As of the ASP.NET Core 3.2 Preview 3 release ([current release is 3.2 Preview 4](xref:blazor/get-started)), Blazor WebAssembly supports configuration from:
95+
Blazor WebAssembly supports configuration from:
9696

97-
* *wwwroot/appsettings.json*
98-
* *wwwroot/appsettings.{ENVIRONMENT}.json*
97+
* The [File Configuration Provider](xref:fundamentals/configuration/index#file-configuration-provider) for app settings files by default:
98+
* *wwwroot/appsettings.json*
99+
* *wwwroot/appsettings.{ENVIRONMENT}.json*
100+
* Other [configuration providers](xref:fundamentals/configuration/index) registered by the app.
99101

100-
Add an *appsettings.json* file in the *wwwroot* folder:
102+
> [!WARNING]
103+
> Configuration in a Blazor WebAssembly app is visible to users. **Don't store app secrets or credentials in configuration.**
104+
105+
For more information on configuration providers, see <xref:fundamentals/configuration/index>.
106+
107+
#### App settings configuration
108+
109+
*wwwroot/appsettings.json*:
101110

102111
```json
103112
{
@@ -117,8 +126,113 @@ Inject an <xref:Microsoft.Extensions.Configuration.IConfiguration> instance into
117126
<p>Message: @Configuration["message"]</p>
118127
```
119128

120-
> [!WARNING]
121-
> Configuration in a Blazor WebAssembly app is visible to users. **Don't store app secrets or credentials in configuration.**
129+
#### Provider configuration
130+
131+
The following example uses a <xref:Microsoft.Extensions.Configuration.Memory.MemoryConfigurationSource> and the [File Configuration Provider](xref:fundamentals/configuration/index#file-configuration-provider) to supply additional configuration:
132+
133+
`Program.Main`:
134+
135+
```csharp
136+
using Microsoft.Extensions.Configuration;
137+
138+
...
139+
140+
var vehicleData = new Dictionary<string, string>()
141+
{
142+
{ "color", "blue" },
143+
{ "type", "car" },
144+
{ "wheels:count", "3" },
145+
{ "wheels:brand", "Blazin" },
146+
{ "wheels:brand:type", "rally" },
147+
{ "wheels:year", "2008" },
148+
};
149+
150+
var memoryConfig = new MemoryConfigurationSource { InitialData = vehicleData };
151+
152+
...
153+
154+
builder.Configuration
155+
.Add(memoryConfig)
156+
.AddJsonFile("cars.json", optional: false, reloadOnChange: true);
157+
```
158+
159+
Inject an <xref:Microsoft.Extensions.Configuration.IConfiguration> instance into a component to access the configuration data:
160+
161+
```razor
162+
@page "/"
163+
@using Microsoft.Extensions.Configuration
164+
@inject IConfiguration Configuration
165+
166+
<h1>Configuration example</h1>
167+
168+
<h2>Wheels</h2>
169+
170+
<ul>
171+
<li>Count: @Configuration["wheels:count"]</p>
172+
<li>Brand: @Configuration["wheels:brand"]</p>
173+
<li>Type: @Configuration["wheels:brand:type"]</p>
174+
<li>Year: @Configuration["wheels:year"]</p>
175+
</ul>
176+
177+
@code {
178+
var wheelsSection = Configuration.GetSection("wheels");
179+
180+
...
181+
}
182+
```
183+
184+
#### Authentication configuration
185+
186+
*wwwroot/appsettings.json*:
187+
188+
```json
189+
{
190+
"AzureAD": {
191+
"Authority": "https://login.microsoftonline.com/",
192+
"ClientId": "aeaebf0f-d416-4d92-a08f-e1d5b51fc494"
193+
}
194+
}
195+
```
196+
197+
`Program.Main`:
198+
199+
```csharp
200+
builder.Services.AddOidcAuthentication(options =>
201+
builder.Configuration.Bind("AzureAD", options);
202+
```
203+
204+
#### Logging configuration
205+
206+
*wwwroot/appsettings.json*:
207+
208+
```json
209+
{
210+
"Logging": {
211+
"LogLevel": {
212+
"Default": "Information",
213+
"Microsoft": "Warning",
214+
"Microsoft.Hosting.Lifetime": "Information"
215+
}
216+
}
217+
}
218+
```
219+
220+
`Program.Main`:
221+
222+
```csharp
223+
builder.Logging.AddConfiguration(
224+
builder.Configuration.GetSection("Logging"));
225+
```
226+
227+
#### Host builder configuration
228+
229+
`Program.Main`:
230+
231+
```csharp
232+
var hostname = builder.Configuration["HostName"];
233+
```
234+
235+
#### Cached configuration
122236

123237
Configuration files are cached for offline use. With [Progressive Web Applications (PWAs)](xref:blazor/progressive-web-app), you can only update configuration files when creating a new deployment. Editing configuration files between deployments has no effect because:
124238

0 commit comments

Comments
 (0)