Skip to content

Commit 0306fec

Browse files
authored
Blazor WASM config file+other providers updates (#18127)
1 parent 4b119ec commit 0306fec

1 file changed

Lines changed: 41 additions & 13 deletions

File tree

aspnetcore/blazor/hosting-model-configuration.md

Lines changed: 41 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ description: Learn about Blazor hosting model configuration, including how to in
55
monikerRange: '>= aspnetcore-3.1'
66
ms.author: riande
77
ms.custom: mvc
8-
ms.date: 04/25/2020
8+
ms.date: 05/04/2020
99
no-loc: [Blazor, "Identity", "Let's Encrypt", Razor, SignalR]
1010
uid: blazor/hosting-model-configuration
1111
---
@@ -92,12 +92,12 @@ The `IWebAssemblyHostEnvironment.BaseAddress` property can be used during startu
9292

9393
### Configuration
9494

95-
Blazor WebAssembly supports configuration from:
95+
Blazor WebAssembly loads configuration from:
9696

97-
* The [File Configuration Provider](xref:fundamentals/configuration/index#file-configuration-provider) for app settings files by default:
97+
* App settings files by default:
9898
* *wwwroot/appsettings.json*
9999
* *wwwroot/appsettings.{ENVIRONMENT}.json*
100-
* Other [configuration providers](xref:fundamentals/configuration/index) registered by the app.
100+
* Other [configuration providers](xref:fundamentals/configuration/index) registered by the app. Not all providers are appropriate for Blazor WebAssembly apps. Clarification on which providers are supported for Blazor WebAssembly is tracked by [Clarify configuration providers for Blazor WASM (dotnet/AspNetCore.Docs #18134)](https://github.com/dotnet/AspNetCore.Docs/issues/18134).
101101

102102
> [!WARNING]
103103
> Configuration in a Blazor WebAssembly app is visible to users. **Don't store app secrets or credentials in configuration.**
@@ -128,12 +128,12 @@ Inject an <xref:Microsoft.Extensions.Configuration.IConfiguration> instance into
128128

129129
#### Provider configuration
130130

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:
131+
The following example uses a <xref:Microsoft.Extensions.Configuration.Memory.MemoryConfigurationSource> to supply additional configuration:
132132

133133
`Program.Main`:
134134

135135
```csharp
136-
using Microsoft.Extensions.Configuration;
136+
using Microsoft.Extensions.Configuration.Memory;
137137

138138
...
139139

@@ -151,9 +151,7 @@ var memoryConfig = new MemoryConfigurationSource { InitialData = vehicleData };
151151

152152
...
153153

154-
builder.Configuration
155-
.Add(memoryConfig)
156-
.AddJsonFile("cars.json", optional: false, reloadOnChange: true);
154+
builder.Configuration.Add(memoryConfig);
157155
```
158156

159157
Inject an <xref:Microsoft.Extensions.Configuration.IConfiguration> instance into a component to access the configuration data:
@@ -168,10 +166,10 @@ Inject an <xref:Microsoft.Extensions.Configuration.IConfiguration> instance into
168166
<h2>Wheels</h2>
169167
170168
<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>
169+
<li>Count: @Configuration["wheels:count"]</li>
170+
<li>Brand: @Configuration["wheels:brand"]</li>
171+
<li>Type: @Configuration["wheels:brand:type"]</li>
172+
<li>Year: @Configuration["wheels:year"]</li>
175173
</ul>
176174
177175
@code {
@@ -181,6 +179,36 @@ Inject an <xref:Microsoft.Extensions.Configuration.IConfiguration> instance into
181179
}
182180
```
183181

182+
To read other configuration files from the *wwwroot* folder into configuration, use an `HttpClient` to obtain the file's content. When using this approach, the existing `HttpClient` service registration can use the local client created to read the file, as the following example shows:
183+
184+
*wwwroot/cars.json*:
185+
186+
```json
187+
{
188+
"size": "tiny"
189+
}
190+
```
191+
192+
`Program.Main`:
193+
194+
```csharp
195+
using Microsoft.Extensions.Configuration;
196+
197+
...
198+
199+
var client = new HttpClient()
200+
{
201+
BaseAddress = new Uri(builder.HostEnvironment.BaseAddress)
202+
};
203+
204+
builder.Services.AddTransient(sp => client);
205+
206+
using var response = await client.GetAsync("cars.json");
207+
using var stream = await response.Content.ReadAsStreamAsync();
208+
209+
builder.Configuration.AddJsonStream(stream);
210+
```
211+
184212
#### Authentication configuration
185213

186214
*wwwroot/appsettings.json*:

0 commit comments

Comments
 (0)