# Working with App Settings and Azure App Services

# Intro

We've recently created a web app (opens new window) and uploaded it to Azure App Service. We also took a look at multiple ways (opens new window) to examine those files through the Azure portal interface and how we'd add extensions (opens new window) and deployment slots (opens new window) for our web app. We also took a look at a feature called Testing in Production (opens new window) and how to use PowerShell (opens new window) to automatically distribute the load between your Production and deployment slot. We wrapped up by looking at the Azure Resource Explorer (opens new window) to explore REST APIs and how to perform load testing (opens new window). In this post, we'll take advantage of App Settings to store a Key/Value pair securely in Azure and access it in your web app.

# Working with App Settings and Azure App Services

App Settings are used to store configurable items without making any changes to the code. The key-value pairs are stored behind the scenes in a configuration store which is nice because sensitive information never shows up in a web.config, etc. file. In order to take advantage of this, you'll need to log into your Azure account and go to your App Service that you created and look under Development Tools then you will see Application Settings. Open it and scroll down and you'll see App Settings as shown below.

We're going to add an App Setting in Azure. I added one with the key of Environment and the value is set to Staging

Open or create your ASP.NET MVC app and modify the appSettings section of the web.config file to add our Environment key/value pair as shown below :

  <appSettings>
    <add key="webpages:Version" value="3.0.0.0" />
    <add key="webpages:Enabled" value="false" />
    <add key="Environment" value="Production" />
  </appSettings>
1
2
3
4
5

Now it is in our web configuration of our app. In order to see the value, we'll need to add it to a page to display the value.

If you're using ASP.NET MVC, then navigate to your appname/Views/Home/Index.cshtml file and add the following using statement followed by a call to ConfigurationManager as shown below :

@using System.Configuration
@{
    ViewBag.Title = "Home Page";
}

<div class="jumbotron')">
    <h1>Testing App Settings</h1>
    @ConfigurationManager.AppSettings["Environment"]
</div>

1
2
3
4
5
6
7
8
9
10

If you run the application locally, then you'll see Production as it is coming from the web.config file, but if you run it inside of Azure, then you'll see Staging as it is coming from the App Settings configuration store located in Azure. Neat stuff!