3.6 KiB
title | author | description | monikerRange | ms.author | ms.custom | ms.date | no-loc | uid | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
ASP.NET Core Blazor environments | guardrex | Learn about environments in Blazor, including how to set the environment of a Blazor WebAssembly app. | >= aspnetcore-3.1 | riande | mvc | 06/10/2020 |
|
blazor/fundamentals/environments |
ASP.NET Core Blazor environments
[!NOTE] This topic applies to Blazor WebAssembly. For general guidance on ASP.NET Core app configuration, see xref:fundamentals/environments.
When running an app locally, the environment defaults to Development. When the app is published, the environment defaults to Production.
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.
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.
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:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
...
<httpProtocol>
<customHeaders>
<add name="blazor-environment" value="Staging" />
</customHeaders>
</httpProtocol>
</system.webServer>
</configuration>
[!NOTE] To use a custom
web.config
file for IIS that isn't overwritten when the app is published to thepublish
folder, see xref:blazor/host-and-deploy/webassembly#use-a-custom-webconfig.
Obtain the app's environment in a component by injecting xref:Microsoft.AspNetCore.Components.WebAssembly.Hosting.IWebAssemblyHostEnvironment and reading the xref:Microsoft.AspNetCore.Components.WebAssembly.Hosting.IWebAssemblyHostEnvironment.Environment property:
@page "/"
@using Microsoft.AspNetCore.Components.WebAssembly.Hosting
@inject IWebAssemblyHostEnvironment HostEnvironment
<h1>Environment example</h1>
<p>Environment: @HostEnvironment.Environment</p>
During startup, the xref:Microsoft.AspNetCore.Components.WebAssembly.Hosting.WebAssemblyHostBuilder exposes the xref:Microsoft.AspNetCore.Components.WebAssembly.Hosting.IWebAssemblyHostEnvironment through the xref:Microsoft.AspNetCore.Components.WebAssembly.Hosting.WebAssemblyHostBuilder.HostEnvironment property, which enables developers to have environment-specific logic in their code:
if (builder.HostEnvironment.Environment == "Custom")
{
...
};
The following convenience extension methods permit checking the current environment for Development, Production, Staging, and custom environment names:
IsDevelopment()
IsProduction()
IsStaging()
IsEnvironment("{ENVIRONMENT NAME}")
if (builder.HostEnvironment.IsStaging())
{
...
};
if (builder.HostEnvironment.IsEnvironment("Custom"))
{
...
};
The xref:Microsoft.AspNetCore.Components.WebAssembly.Hosting.IWebAssemblyHostEnvironment.BaseAddress?displayProperty=nameWithType property can be used during startup when the xref:Microsoft.AspNetCore.Components.NavigationManager service isn't available.