Blazor Server: Can't change ASPNETCORE_ENVIRONMENT

54 Views Asked by At

I have a Blazor Server application with a set of custom environment variables, I am trying to set the environment into DevelopmentLocal on the web.config file, however It stays on the Development environment

This is the list of appsettings.json

List of appsettings

Program.cs

...
Console.WriteLine($"This application is running on the environment: {app.Environment.EnvironmentName}");
// Output: This application is running on the environment: Development
// Expected: This application is running on the environment: DevelopmentLocal
...

web.config

...
      <environmentVariables>
        <environmentVariable name="ASPNETCORE_ENVIRONMENT" value="DevelopmentLocal"/>
      </environmentVariables>
...
2

There are 2 best solutions below

0
Gabriel Ribeiro Rossi On

Check your launchSettings.json file.

enter image description here

0
Fengzhi Zhou On

Modifying the launchsettings only works for VS, and in code level, you could add the variables in program.cs

Add the variables in your program.cs

var builder = WebApplication.CreateBuilder(args);

builder.Host.UseContentRoot(Directory.GetCurrentDirectory());

builder.Configuration
    .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
    .AddJsonFile($"appsettings.{builder.Environment.EnvironmentName}.json", optional: true)
    .AddEnvironmentVariables();
...

Or you can set the env before running the project

Set in cmd setx ASPNETCORE_ENVIRONMENT "DevelopmentLocal"

enter image description here

enter image description here