Get variables from web.config and global.asax


Saturday, December 5, 2009


There are two methods, you can assign variable at one place and call it from through out the website.

The first is, Declare the variable in web.config and call it in any aspx.cs page and the second is, Declare the variable in global.asax and call it in any aspx.cs page.

The first one is very usefull because you can edit it directly form the FTP editor.

Please find explanation and code of both method below,


First Method:
web.config
<appSettings>
<add key="siteurl" value="www.indianic.com"/>
<add key="TempDir" value="./uploads"/>
</appSettings>

Default.aspx.cs
ConfigurationSettings.AppSettings["siteurl"].ToString();
ConfigurationSettings.AppSettings["TempDir"].ToString();


Second Method:
global.asax
void Session_Start(object sender, EventArgs e)
{
// Code that runs when a new session is started
Application["siteurl"] = "www.indianic.com";
Application["TempDir"] = "./uploads";
}

Default.aspx.cs
string smtphost = Application["siteurl"].ToString();
string userid = Application["TempDir"].ToString();


Please use any of the above method which is needed in your application.

Hope this post will help you,
if yes please put comment below of this page,
Rajesh Singh,
Asp.Net Developer
Indianic Infotech Ltd (India)
rajesh@indianic.com

No comments :