How to access a variable from the web.config file using C#

The web.config file can be a handy place to put site wide settings like the website name or contact admin email address. The steps below will let you use your web.config file to store and retrieve those details. This example will show you how to set the website name in the web.config, create a class to access that name and display the name in page title.

First up we need to store the values in the web.config file itself so open up your web.config file in your project and locate the <appSettings /> tag. You want your appSettings element to look something like this:

<appSettings>
    <add key="SiteName" value="My New Site" />
 </appSettings>

So now you have the name for your site set in the web.config. But how do we actually get this information into our pages? Well the good news is that it's quite easy! I'm going to make a new class called 'sitesettings.cs' in my App_Code folder and inside in that put the following code:

using System;
using System.Collections.Generic;
using System.Web;
//this will allow you to pickup the web.config values
using System.Web.Configuration;
/// 
/// Gets the website settings stored in the web.config file
/// 
public class WebsiteSettings
{
public WebsiteSettings()
{
}
//get the web site title
public static string SiteTitle
{
get
{
return WebConfigurationManager.AppSettings["SiteName"];
}
}
}

Once you have the code above saved as a new class you can access the values within your .aspx.cs pages with ease. For example, if you want to print out the website name as a page title you simple need to include the following line in your code behind page (this example is for an aspx page that uses a MasterPage):

protected void Page_Load(object sender, EventArgs e)
{
//line below will read the site name from webconfig
//and display "My New Site - home page" in your browser window title
Master.Page.Title = WebsiteSettings.SiteTitle + " - home page";
}

Obviously this is only a quick example of what you can do but it can be a handy way to store some of your site settings like the site name, url links, admin email address, etc.

Tags: ,

Comments

7/30/2009 10:28:31 PM #

Tony

Hi, is there a way to access it via a variable in the aspx page?

Such as, could the root url of http://www.mywebsite/page1.aspx be written in my aspx page code as <%= SiteUrl %> /page1.aspx?

Tony |

7/31/2009 9:34:30 AM #

richard reddy

Hi Tony,

Make sure you include this line in your code behind page:
using System.Web.Configuration;

If  I want to write out the site name on the screen using an ASP:Literal control I would also add the following in the code behind:

SiteName.Text = WebConfigurationManager.AppSettings["SiteName"];

And finally on the front .aspx page I just add:
<asp:Literal ID="SiteName" runat="server" />

Hope that makes sense to you.

Rich

richard reddy |

8/5/2009 5:17:51 PM #

Tony

Thanks Rich, I can certainly make use of that for some things.

Tony |

Comments are closed