Skip to content

Commit d8b3d89

Browse files
authored
Surface additional Blazor environment content (#17584)
* Surface additional Blazor environment content * Nits * NOTE on using a custom web.config
2 parents 052119e + d5940b4 commit d8b3d89

1 file changed

Lines changed: 62 additions & 3 deletions

File tree

aspnetcore/blazor/hosting-model-configuration.md

Lines changed: 62 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/07/2020
99
no-loc: [Blazor, SignalR]
1010
uid: blazor/hosting-model-configuration
1111
---
@@ -19,14 +19,73 @@ 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+
> [!NOTE]
49+
> To use a custom *web.config* file for IIS that isn't overwritten when the app is published to the *publish* folder, see <xref:host-and-deploy/blazor/webassembly#use-a-custom-webconfig>.
50+
51+
Obtain the app's environment in a component by injecting `IWebAssemblyHostEnvironment` and reading the `Environment` property:
52+
53+
```razor
54+
@page "/"
55+
@using Microsoft.AspNetCore.Components.WebAssembly.Hosting
56+
@inject IWebAssemblyHostEnvironment HostEnvironment
57+
58+
<h1>Environment example</h1>
59+
60+
<p>Environment: @HostEnvironment.Environment</p>
61+
```
62+
63+
### Configuration
64+
2265
As of the ASP.NET Core 3.2 Preview 3 release, Blazor WebAssembly supports configuration from:
2366

2467
* *wwwroot/appsettings.json*
2568
* *wwwroot/appsettings.{ENVIRONMENT}.json*
2669

27-
In a Blazor Hosted app, the [runtime environment](xref:fundamentals/environments) is the same as the server app's value.
70+
Add an *appsettings.json* file in the *wwwroot* folder:
71+
72+
```json
73+
{
74+
"message": "Hello from config!"
75+
}
76+
```
77+
78+
Inject an <xref:Microsoft.Extensions.Configuration.IConfiguration> instance into a component to access the configuration data:
2879

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>.
80+
```razor
81+
@page "/"
82+
@using Microsoft.Extensions.Configuration
83+
@inject IConfiguration Configuration
84+
85+
<h1>Configuration example</h1>
86+
87+
<p>Message: @Configuration["message"]</p>
88+
```
3089

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

0 commit comments

Comments
 (0)