Azure App Service - Web Config Vs Application Settings

Introduction

Every .NET developer may have used the following tags available in the Web.Config file while developing web applications, using the ASP.NET Framework.

  • AppSettings
  • ConnectionStrings

AppSettings

These are used to store some configurable items. Below are a few of the examples.

  • SMTPServer Details

    Create configurable values for SMTP Server, Username, and Password. In production, you can even store encrypted passwords in Web.Config.

  • Path

    Path of some template files or log files that could change in different environments.

You could use AppSettings key for storing anything that you would like to configure later, without making any changes to the code.

ConnectionStrings

These are used for storing the complete ConnectionStrings.

For example - The ConnectionString of SQL Server.

Both the above tags and related attributes are very helpful for developers, architects, and IT Administrators for configuring different values for different environments.

However, one of the disadvantages of these files is that as an IT Administrator or Architect, you would need to understand the syntax of these tags along with their attributes. You might not have the required tools to use the advantages, like Intellisense in the staging or production environments. As a result, you need to depend on your developers to make changes to the configuration files each time you want to deploy on a new environment.

Sometimes, you might not like to reveal your credentials to developers. In this article, we will learn how to overcome this issue using the AppSettings and ConnectionStrings settings of the Application Settings blade of the Azure App Service.

Below is an example of how to use AppSettings in an MVC application.

  • Declare an AppSetting in Web.Config file.
    1. <appSettings>  
    2.    <add key="SMTPServerName" value="MySMTPServerfromWeb.Config"/>  
    3. </appSettings>  
  • Use the App Setting variable in your Action Method – Please note that for simplicity.
    1. public string Index() {  
    2.     ViewBag.Title = "Azure Application Settings - Example";  
    3.     return "This is the Configurable Value in Web.Config for my SMTP Server: " + ConfigurationManager.AppSettings["SMTPServerName"];  
    4. }  

Below is the output of the View.


Let’s deploy the application to an Azure App Service using the steps explained in the following article.

Once you deploy the application, please navigate to the Application Settings blade by clicking on the Application Settings.



Clicking on the Application Settings will open the Application Settings blade. Please scroll down a bit to view the AppSettings and the ConnectionString Settings, as shown below.


This section allows you to configure a different App Setting value than that of the value specified in the Web.Config.

Let’s give some other value to the “SMTPServerName” key, as shown below.


Just to refresh, below are the values that we have provided for SMTPServerName key.

  • Web.Config - MySMTPServerfromWeb.Config
  • Azure App Service – Application Settings - MySMTPServerfromAzureAppSettings

Once you are done, please click on "Save" button.




Azure Management portal will save the changes in just a few seconds. After the changes are saved, let’s navigate to the App Service URL by clicking on the link shown below.



Below is the output of the Azure App Service.


Please note that it has pulled the value from the App Settings configured in the Azure App Services – Application Settings blade but not from the Web.config file. This way, you can change these configurable values whenever you want to without depending on developers. The same rule applies even for Connection String.

In this article, we have learned how the AppSettings and ConnectionString settings work in the Application Settings blade.

Hope you enjoyed reading the article. Your feedback is appreciated.


Similar Articles