Read Configuration Settings of Web.config using Javascript
Posted by Praveen Kumar on April 5, 2008
Web.config is a XML file containing the configuration settings of our application. Amongst its other uses, the file is extremely handy when it comes to changing configuration settings of an application, even when the application is live. You can define your own specific application settings, such as a database connection string using the <appSettings> tag and read and write values to the file programmatically. In this article, we see how to read the configuration settings in the web.config using ‘JavaScript’.
ASP.NET provides the Web Site Administration Tool to view and manage the configuration settings for your ASP.NET website. These configuration settings are stored in an xml file called web.config.
“web.config is an application configuration file used to define configuration settings such as connecting strings, authentication, authorization etc., for an ASP.NET application”
In this short article, I will see how we can retrieve the values from the <appSettings> and <connectionStrings> section in the web.config using JavaScript. So let us get started.
Step 1: Create a new ASP.NET website. Add a button control to the Default.aspx.
Step 2: Right click the project > Add New Item > Web Configuration File
Add the following sample entries in the file between the <configuration> tag as shown below:
<configuration>
<appSettings>
<addkey=“var1“value=“SomeValue“/>
</appSettings>
<connectionStrings>
<addname=“MyConnString“connectionString=“Data Source=(local);Initial Catalog=Northwind;Integrated Security=True;“/>
</connectionStrings>
…
<configuration>
Step 3: We will now read these entries using JavaScript. To do so, add the following script in the <head> tag of your Default.aspx page as shown below:
<head runat=”server”>
<title>Read Config Entries Using Javascript</title>
<script type=”text/javascript”>
function ReadConfigSettings()
{
var conn = ‘<%=ConfigurationManager.ConnectionStrings["MyConnString"].ConnectionString %>’
alert(conn);
var v1 = ‘<%=ConfigurationManager.AppSettings["var1"].ToString() %>’
alert(v1);
}
</script>
</head>
Step 4: Call this function on a button click and display the values of the configuration settings
<asp:Button ID=”Button1″ runat=”server” Text=”Button”OnClientClick=”ReadConfigSettings()” /></div>
That’s it. Run the application and click the button. The values of the configuration settings in the web.config will be displayed in the alert boxes.
I hope you liked this short article and I thank you for viewing it. If you liked the article,
then please comment it or give me suggestion to make more effective.

Satya said
praveen,
I have tried the following code in my js file. It is not working. I am using vs.net 2005.
var v1 = ”
alert(v1);
praveensunsetpoint said
Hi Satya, What value you are getting in v1??
Could you send me your code??
Vinayak said
i am using sme code given here in vs2005 it is giving me error
identifier expected
Vinayak said
Code for ur ref
Untitled Page
function ReadConfigSettings()
{
var v1 = ‘’
alert(v1);
}
praveensunsetpoint said
I think you are doing mistake in closing mean ‘ or ” you just check it out. could you please send me the line which encountered to error.
cosa_Netlight said
A js-file is not compiled by the server. Thus, this code will not work for you. Place your javascript inline in an aspx-file or use another method (reading XML files with javascript).
shyam said
Very good And Excellent . you are doing great job. thank you
jj said
its no working for me either. its returning the string to the right of the equal sign as it is
Praveen Kumar said
Hello jj,
Do not copy paste the code cause it paste the wild character.
Just write the code manually.
Thanks
Todd said
This does not work
Praveen Kumar said
Hi Todd, you should explain some more to get us know what is not working??
Thanks
Praveen
Thilina said
It didnt works for so insted of “ConfigurationManager.ConnectionStrings” I change as bellow
it works… Thanks
sorry this was missing
“var conn = ”;”
alert(conn);
Thilina said
This is what I am trying to put. (Every time it ignores, so I convert as string, so syntax not correct)
“System.Configuration.ConfigurationManager.AppSettings["sessionDir"].ToString()”
mfchiu said
var X = ”
alert(X);