Problem


An application needs to know if itself is running in Azure App Service, so that it can apply special logic and optimization for Azure. 

Solution


When deploying an application to Azure App Service, the platform automatically assigns a set of environment variables that can be accessed by the application code.

To view the complete list of environment variables applicable to your application at runtime, you can navigate to the "Advanced Tools" blade within your App Service Instance.

Then, go to "Environment" page. You will see the entire list here. Among them, are a few environment variables that are suitable for identify if the application is running on Azure App Service.

  • WEBSITE_SKU
  • WEBSITE_SITE_NAME
  • WEBSITE_HOSTNAME

Now, in our code, we can try to get the values of those environment variables, example code for ASP.NET Core Razor Page is like:

<tr>
    <td>@SharedLocalizer["Azure App Service"]</td>
    <td class="text-muted">
        WEBSITE_SKU : @Environment.GetEnvironmentVariable("WEBSITE_SKU") <br />
        WEBSITE_SITE_NAME: @Environment.GetEnvironmentVariable("WEBSITE_SITE_NAME") <br />
        WEBSITE_HOSTNAME : @Environment.GetEnvironmentVariable("WEBSITE_HOSTNAME") <br />
        REGION_NAME  : @Environment.GetEnvironmentVariable("REGION_NAME") <br />
    </td>
</tr>

Example result: