AspNetCore.Docs/aspnetcore/blazor/fundamentals/environments.md

4.7 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 12/11/2020
Home
Privacy
Kestrel
appsettings.json
ASP.NET Core Identity
cookie
Cookie
Blazor
Blazor Server
Blazor WebAssembly
Identity
Let's Encrypt
Razor
SignalR
blazor/fundamentals/environments

ASP.NET Core Blazor environments

[!NOTE] This topic applies to Blazor WebAssembly. For general guidance on ASP.NET Core app configuration, which describes the approaches to use for Blazor Server apps, see xref:fundamentals/environments.

When running an app locally, the environment defaults to Development. When the app is published, the environment defaults to Production.

The client-side Blazor app (Client) of a hosted Blazor WebAssembly solution determines the environment from the Server app of the solution via a middleware that communicates the environment to the browser. The Server app adds a header named blazor-environment with the environment as the value of the header. The Client app reads the header. The Server app of the solution is an ASP.NET Core app, so more information on how to configure the environment is found in xref:fundamentals/environments.

For a standalone Blazor WebAssembly 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, the custom header (blazor-environment) is added to the published web.config file. The web.config file is located in the bin/Release/{TARGET FRAMEWORK}/publish folder, where the placeholder {TARGET FRAMEWORK} is the target framework:

<?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 the publish 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.

Pages/ReadEnvironment.razor:

::: moniker range=">= aspnetcore-5.0"

[!code-razor]

::: moniker-end

::: moniker range="< aspnetcore-5.0"

[!code-razor]

::: moniker-end

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 environment-specific logic in host builder code.

In Program.Main of Program.cs:

if (builder.HostEnvironment.Environment == "Custom")
{
    ...
};

The following convenience extension methods provided through xref:Microsoft.AspNetCore.Components.WebAssembly.Hosting.WebAssemblyHostEnvironmentExtensions permit checking the current environment for Development, Production, Staging, and custom environment names:

In Program.Main of Program.cs:

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.

Additional resources