9
9
using Microsoft . AspNetCore . StaticFiles ;
10
10
using Microsoft . Extensions . DependencyInjection ;
11
11
using Microsoft . Extensions . FileProviders ;
12
- using Microsoft . Extensions . Hosting ;
13
12
using Microsoft . Extensions . Primitives ;
14
13
using Microsoft . Net . Http . Headers ;
15
14
@@ -20,6 +19,12 @@ namespace Microsoft.AspNetCore.Builder;
20
19
/// </summary>
21
20
public static class ComponentsWebAssemblyApplicationBuilderExtensions
22
21
{
22
+ private static readonly string ? s_dotnetModifiableAssemblies = GetNonEmptyEnvironmentVariableValue ( "DOTNET_MODIFIABLE_ASSEMBLIES" ) ;
23
+ private static readonly string ? s_aspnetcoreBrowserTools = GetNonEmptyEnvironmentVariableValue ( "__ASPNETCORE_BROWSER_TOOLS" ) ;
24
+
25
+ private static string ? GetNonEmptyEnvironmentVariableValue ( string name )
26
+ => Environment . GetEnvironmentVariable ( name ) is { Length : > 0 } value ? value : null ;
27
+
23
28
/// <summary>
24
29
/// Configures the application to serve Blazor WebAssembly framework files from the path <paramref name="pathPrefix"/>. This path must correspond to a referenced Blazor WebAssembly application project.
25
30
/// </summary>
@@ -42,23 +47,19 @@ public static IApplicationBuilder UseBlazorFrameworkFiles(this IApplicationBuild
42
47
{
43
48
context . Response . Headers . Append ( "Blazor-Environment" , webHostEnvironment . EnvironmentName ) ;
44
49
45
- if ( webHostEnvironment . IsDevelopment ( ) )
50
+ // DOTNET_MODIFIABLE_ASSEMBLIES is used by the runtime to initialize hot-reload specific environment variables and is configured
51
+ // by the launching process (dotnet-watch / Visual Studio).
52
+ // Always add the header if the environment variable is set, regardless of the kind of environment.
53
+ if ( s_dotnetModifiableAssemblies != null )
54
+ {
55
+ context . Response . Headers . Append ( "DOTNET-MODIFIABLE-ASSEMBLIES" , s_dotnetModifiableAssemblies ) ;
56
+ }
57
+
58
+ // See https://github.com/dotnet/aspnetcore/issues/37357#issuecomment-941237000
59
+ // Translate the _ASPNETCORE_BROWSER_TOOLS environment configured by the browser tools agent in to a HTTP response header.
60
+ if ( s_aspnetcoreBrowserTools != null )
46
61
{
47
- // DOTNET_MODIFIABLE_ASSEMBLIES is used by the runtime to initialize hot-reload specific environment variables and is configured
48
- // by the launching process (dotnet-watch / Visual Studio).
49
- // In Development, we'll transmit the environment variable to WebAssembly as a HTTP header. The bootstrapping code will read the header
50
- // and configure it as env variable for the wasm app.
51
- if ( Environment . GetEnvironmentVariable ( "DOTNET_MODIFIABLE_ASSEMBLIES" ) is string dotnetModifiableAssemblies )
52
- {
53
- context . Response . Headers . Append ( "DOTNET-MODIFIABLE-ASSEMBLIES" , dotnetModifiableAssemblies ) ;
54
- }
55
-
56
- // See https://github.com/dotnet/aspnetcore/issues/37357#issuecomment-941237000
57
- // Translate the _ASPNETCORE_BROWSER_TOOLS environment configured by the browser tools agent in to a HTTP response header.
58
- if ( Environment . GetEnvironmentVariable ( "__ASPNETCORE_BROWSER_TOOLS" ) is string blazorWasmHotReload )
59
- {
60
- context . Response . Headers . Append ( "ASPNETCORE-BROWSER-TOOLS" , blazorWasmHotReload ) ;
61
- }
62
+ context . Response . Headers . Append ( "ASPNETCORE-BROWSER-TOOLS" , s_aspnetcoreBrowserTools ) ;
62
63
}
63
64
64
65
await next ( context ) ;
0 commit comments