1
votes

I have just created a "staging" slot in one of my Azure App Services.

In Azure Portal, inside Application Settings for that Slot, I created a new key, as follow:

Azure-1

...and made it a "Slot Setting" as I don't want this value to be swaped.

When I execute my code in a .NET Core project, Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT") returns null. Locally it works, as soon as I set this value in my computer environment variables.

Am I missing something?

2
Do you have any update about this thread? - Tom Sun - MSFT
@TomSun after following your test steps, I managed to check the environment was not "Staging" as I expected. I deleted the slot, and created it again, then it worked. This is a weird behavior, but thanks for reminding me of this thread, so I could give you a feedback. - Alisson
I am glad to know that it could help you. - Tom Sun - MSFT

2 Answers

4
votes

Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT") to get the application setting should work on the Azure WebApp. I assume that value is overridden by other codes.

You could debug it with following way.

1.Check the staging kudu(https://yousitename-staging.scm.azurewebsites.net/Env.cshtml) to check environment variable ASPNETCORE_ENVIRONMENT.

2.We also could remote debug slot with VS.

The following is my test steps:

1.Create a .net core project.

2.Create a slot for an existing Webpp and appsetting for slot

3.Check the environment variable with kudu tool

4.Add the following code in the index.chtml.cs file

var appsetting = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT");
ViewData["appseting"] = appsetting;

5.in the index.chtml file change the title to appsetting value

@page
@model IndexModel
@{
    ViewData["Title"] = ViewData["appseting"];
}

6.Publish the WebApp to Azure with debug mode

7.Check the title of the home page.

we also could remote debug to check it. enter image description here

0
votes

Not sure where/how you use the Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT"), but in my project I retrieve it differently. I retrieve it in the Startup.cs. Could you try something similar and see if you get it this way?

public Startup(IHostingEnvironment env, ILogger<Startup> logger)
        {

            var envName = env.EnvironmentName;
        }

This should give you the env name in the envName variable. If this works I can help you with how you get it other places in your code.