Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to read Azure web site app settings values

I am trying to configure some key/value pairs for my Azure web application using app settings section on Windows Azure preview portal.

enter image description here

Now I am trying to read values like below

ConfigurationManager.AppSettings["MyWebApp.DbConnectionString"];

but it returns null values.

Reading app settings from Web.config in my web application works fine.

like image 658
Ashutosh B Bodake Avatar asked Jun 14 '17 10:06

Ashutosh B Bodake


People also ask

How do I read application settings in Azure App Service?

App settings can also be resolved from Key Vault using Key Vault references. In the Azure portal, search for and select App Services, and then select your app. In the app's left menu, select Configuration > Application settings.

How do I access Azure web config?

Go to https://someapp.scm.azurewebsites.net/dev . The third thing: if you have any settings in your web. config that you want to manage without updating the configuration file, have a look at the Application Settings. Any setting that's in there overrides the setting with the same name in the web.

How do I check environment variables in Azure?

Azure portal exampleUnder Environment variables, enter NumWords with a value of 5 for the first variable, and enter MinLength with a value of 8 for the second variable. Select Review + create to verify and then deploy the container.


2 Answers

I found the solution.

Keep values in web.config as well as in Azure App setting. When you are running/debugging application on your local environment it picks values from web.config.

When you deploy application on Azure it picks values from App setting.

//Below code work for both.
ConfigurationManager.AppSettings["KeyName"]

Keep key name same in web.config as well as in Azure app setting.

like image 79
Ashutosh B Bodake Avatar answered Oct 06 '22 17:10

Ashutosh B Bodake


In Azure, there are a few different ways of retrieving Application Settings and Connection Strings. However, connection strings work a little differently than vanilla application settings.

Application Settings can be retrieved by any method, regardless of whether or not they are present in the Web.config file.

Connection Strings can also be retrieved by any method if the string is defined in Web.config. However, if the connection string is NOT defined in Web.config, then it can only be retrieved using the Environment Variable method.

Retrieving as Environment Variable

Environment.GetEnvironmentVariable("APPSETTING_my-setting-key");
Environment.GetEnvironmentVariable("SQLAZURECONNSTR_my-connection-string-key");

Note that the keys must be prepended with a string designating their type when using this method.

All Application Settings use the APPSETTING_ prefix.

Connection Strings have a different prefix depending on the type of database selected when creating the string in the portal:

"Sql Databases" --> "SQLAZURECONNSTR_my-connection-string-key"
"SQL Server" --> "SQLCONNSTR_my-connection-string-key"
"MySQL" --> "MYSQLCONNSTR_my-connection-string-key"
"Custom" --> "CUSTOMCONNSTR_my-connection-string-key"

For a full overview, see the Windows Azure Web Sites documentation.

like image 21
Will Avatar answered Oct 06 '22 18:10

Will