blazoraspnetcore-environment

Blazor WASM Environment is always Production


I have a .Net 8 Blazor Web App and the client's builder.HostEnvironment.Environment is always (locally and on Azure) set to 'Production' even if the Server builder.Environment is set to 'Development'

Why? I don't know about anywhere in the client project where I set it to 'Production'.


Solution

  • Using 'DebugLocal' instead of 'Debug' solved it.

    enter image description here

    ---EDIT---

    This solution is not working for me anymore (I think since I've updated to dotnet 8.0.8).

    It looks like the issue is that for some reason, the blazor-environment header is not being set from the server, doing it from code solved it for me.

    Need to insert this code before app.UseStaticFiles();

    var env = app.Environment.EnvironmentName;
    app.Use((context, next) =>
    {
        var url = context.Request.GetEncodedUrl();
        if (url.EndsWith("blazor.boot.json"))
        {
            context.Response.Headers.Append("blazor-environment", env);
        }
    
        return next(context);
    });
    

    For Request.GetEncodedUrl() to work, you need using Microsoft.AspNetCore.Http.Extensions;

    See: https://github.com/dotnet/aspnetcore/issues/25152#issuecomment-2344329700