问题
How can I get the name of the slot (production or staging) of my app service when the asp.net core process starts.
The HTTP_HOST environment variable doesn't appear to be set on startup and I have no http request to inspect.
回答1:
If we want to get the host name, you could use the environment variable WEBSITE_HOSTNAME to do that.
var hostName = Environment.GetEnvironmentVariable("WEBSITE_HOSTNAME");
If you run it on the slot environment then you will get the value youwebsiteName-slotName.azurewebsites.net
. Or if you run it on the production environment you will get the value youwebsiteName.azurewebsites.net
.
Update:
We could use the appsetting slot to do that.
1.Go to the slot webApp and config the appsetting and check slot setting.
2.Please use the following code to get the slot name.
string name = string.Empty;
var sitName = Environment.GetEnvironmentVariable("APPSETTING_WEBSITE_SITE_NAME", EnvironmentVariableTarget.Process);
var slotName = Environment.GetEnvironmentVariable("APPSETTING_KeyName", EnvironmentVariableTarget.Process); //APPSETTING_Test_Slot
if (!string.IsNullOrEmpty(slotName))
{
name = sitName + "-" + slotName;
}
else
{
name = sitName;
}
来源:https://stackoverflow.com/questions/50147803/getting-the-azure-app-service-slot-name-on-startup