skip to Main Content

According to this article from Microsoft, "Production is the default value if DOTNET_ENVIRONMENT and ASPNETCORE_ENVIRONMENT have not been set. Apps deployed to Azure are Production by default." I did not have those values set in the configuration of my Azure App Service, yet in my injected IConfiguration in code I am seeing a value from appsettings.Development.json instead of appsettings.json. I also tried explicitly setting ASPNETCORE_ENVIRONMENT equal to "Production", but the result is the same. What am I doing wrong?

2

Answers


  1. Chosen as BEST ANSWER

    I have discovered the issue. I also had the environment variables set in the web.config from a long time ago (pre-Azure). Removing these old values fixed the problem.

    web.config values


  2. "Production is the default value if DOTNET_ENVIRONMENT and ASPNETCORE_ENVIRONMENT have not been set.

    Yes, the ASPNETCORE_ENVIRONMENT after deploying the Web App to Azure App Service will be Production.

    • You can check that by retrieving the ASPNETCORE_ENVIRONMENT value in .cshtml page.
    @inject Microsoft.AspNetCore.Hosting.IWebHostEnvironment Environment
    <h3>ASPNETCORE ENVIRONMENT: @Environment.EnvironmentName</h3>
    

    Environment Output:

    enter image description here

    • I have set sample App Settings value in appsettings.Development.json file.
      "TestVal": "Value from appsettings.Development.json"
    
    • And Iam retrieving the value in .cshtml page using the below code.
    @inject IConfiguration myconfig;
    <h3>TestVal:  @myconfig["TestVal"]</h3>
    

    Local Output:

    enter image description here

    Deployed App Output:

    As I have set the same in appsettings.json , the value from appsettings.json file is loaded.

    enter image description here

    • If the appsettings.json file does not have the above setting, then the value will be null.

    enter image description here

    • If you set the json file in Program.cs as below, only that file values will be loaded.
    builder.Configuration.AddJsonFile($"appsettings.Development.json", true, true);
    builder.Configuration.AddEnvironmentVariables();
    

    Make sure you have not set the json file as mentioned above.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search