Skip to content

Commit f788250

Browse files
authored
Surface additional Blazor environment content
1 parent 8793347 commit f788250

1 file changed

Lines changed: 59 additions & 3 deletions

File tree

aspnetcore/blazor/hosting-model-configuration.md

Lines changed: 59 additions & 3 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: 03/24/2020
8+
ms.date: 04/02/2020
99
no-loc: [Blazor, SignalR]
1010
uid: blazor/hosting-model-configuration
1111
---
@@ -19,14 +19,70 @@ This article covers hosting model configuration.
1919

2020
## Blazor WebAssembly
2121

22+
### Environment
23+
24+
When running an app locally, the environment defaults to Development. When the app is published, the environment defaults to Production.
25+
26+
A hosted Blazor WebAssembly app picks up the environment from the server via a middleware that communicates the environment to the browser by adding the `blazor-environment` header. The value of the header is the environment. The hosted Blazor app and the server app share the same environment. For more information, including how to configure the environment, see <xref:fundamentals/environments>.
27+
28+
For a standalone app running locally, the development server adds the `blazor-environment` header to specify the Development environment. To specify the environment for other hosting environments, add the `blazor-environment` header.
29+
30+
In the following example for IIS, add the custom header to the published *web.config* file. The *web.config* file is located in the *bin/Release/{TARGET FRAMEWORK}/publish* folder:
31+
32+
```xml
33+
<?xml version="1.0" encoding="UTF-8"?>
34+
<configuration>
35+
<system.webServer>
36+
37+
...
38+
39+
<httpProtocol>
40+
<customHeaders>
41+
<add name="blazor-environment" value="Staging" />
42+
</customHeaders>
43+
</httpProtocol>
44+
</system.webServer>
45+
</configuration>
46+
```
47+
48+
Obtain the app's environment in a component by injecting `IWebAssemblyHostEnvironment` and reading the `Environment` property:
49+
50+
```razor
51+
@page "/"
52+
@using Microsoft.AspNetCore.Components.WebAssembly.Hosting
53+
@inject IWebAssemblyHostEnvironment HostEnvironment
54+
55+
<h1>Environment example</h1>
56+
57+
<p>Environment: @HostEnvironment.Environment</p>
58+
```
59+
60+
### Configuration
61+
2262
As of the ASP.NET Core 3.2 Preview 3 release, Blazor WebAssembly supports configuration from:
2363

2464
* *wwwroot/appsettings.json*
2565
* *wwwroot/appsettings.{ENVIRONMENT}.json*
2666

27-
In a Blazor Hosted app, the [runtime environment](xref:fundamentals/environments) is the same as the server app's value.
67+
Add an *appsettings.json* file in the *wwwroot* folder:
2868

29-
When running the app locally, the environment defaults to Development. When the app is published, the environment defaults to Production. For more information, including how to configure the environment, see <xref:fundamentals/environments>.
69+
```json
70+
{
71+
"message": "Hello from config!"
72+
}
73+
```
74+
75+
Inject an <xref:Microsoft.Extensions.Configuration.IConfiguration> instance into a component to access the configuration data:
76+
77+
```razor
78+
@page "/"
79+
@using Microsoft.Extensions.Configuration
80+
@inject IConfiguration Configuration
81+
82+
<h1>Configuration example</h1>
83+
84+
<p>@Configuration["message"]</p>
85+
```
3086

3187
> [!WARNING]
3288
> Configuration in a Blazor WebAssembly app is visible to users. **Don't store app secrets or credentials in configuration.**

0 commit comments

Comments
 (0)