In the recent post we discussed about Defining and Using Connection Strings in web apps for securing login credentials of database or any crucial thing. Today we will talk about App Settings that is similar to Connection Strings but is used for different purposes. For example, connection strings is used for connecting your Web App to Database or Server or any similar thing, but App Settings is used to define the mechanism of the Web Apps. You might want to change the behavior of your Web App (or something same like that) then you can probably go with App Settings in Azure.
Today I will show you how to change color of Label in ASP.NET basic site. This gives you a good demonstration and gives you THE best idea of flexibility of Azure in Web Apps.
Note: I do have a basic ASP.NET site for this demo. You might need to start from new project.
Defining App Settings in Azure Web App
Step 1: In your Azure Portal go to your website’s dashboard then click on Configure Tab.
Figure 1: Go to Configure Tab
Step 2: Scroll down to App Settings.
Step 3: In the list, type a name for the setting in the text box with placeholder text KEY and type a value for the setting in the text box with the placeholder text VALUE.
Figure 2: Defining App Settings
Step 4: Click SAVE on the command bar to apply all the changes.
Figure 3: Click Save
Using App Settings from .NET
Now that we have created and saved app settings for the color of label. Now we will use it in our ASP.NET site which I created earlier. I assume you have already created an Azure .NET Web App or ASP.NET site.
Step 1: Open your project and your page where you want the connection string to be retrieved (I have it on default.aspx).
Step 2: Enter the following code snippet in default.aspx.cs page.
- using System;
- using System.Configuration;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Web.UI;
- using System.Web.UI.WebControls;
-
- namespace ravitestwebsite
- {
- public partial class _default : System.Web.UI.Page
- {
- protected void Page_Load(object sender, EventArgs e)
- {
- string key = "labelColor";
- string colorValue = ConfigurationManager.AppSettings[key];
- label.Text = "I got my color from Azure.";
- label.ForeColor = System.Drawing.Color.FromName(colorValue);
- }
- }
- }
In default.aspx page add a label in the site as in the following.
- <asp:Label runat="server" id="label"></asp:Label>
Step 3: Now publish your site to Azure and run it.
Figure 4: Deployed Site
Let’s change the color and deploy it and see the changes.
Figure 5: Value changed
Figure 6: Color changed
See guys how easy it is to use app settings. This is a basic example. Just think in what ways this can be used. The power of Azure in unbelievable. If you got any query or any idea regarding how to use this comment down below.
Cheers!