Read Configuration Settings of Web.config using Javascript

Posted by: Suprotim Agarwal , on 3/25/2008, in Category ASP.NET
Views: 132487
Abstract: 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 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’.
Read Configuration Settings of Web.config using Javascript
 
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’.
In one of the previous articles, we saw how we can use the API’s in the System.Configuration and System.Web.Configuration namespaces to read and write configuration settings in the web.config. In this short article, we 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 would like to thank JRICE from the ASP.NET forums for this tip.
I hope you liked this short article and I thank you for viewing it. If you liked the article,  Subscribe to my RSS Feed
 
 

This article has been editorially reviewed by Suprotim Agarwal.

Absolutely Awesome Book on C# and .NET

C# and .NET have been around for a very long time, but their constant growth means there’s always more to learn.

We at DotNetCurry are very excited to announce The Absolutely Awesome Book on C# and .NET. This is a 500 pages concise technical eBook available in PDF, ePub (iPad), and Mobi (Kindle).

Organized around concepts, this Book aims to provide a concise, yet solid foundation in C# and .NET, covering C# 6.0, C# 7.0 and .NET Core, with chapters on the latest .NET Core 3.0, .NET Standard and C# 8.0 (final release) too. Use these concepts to deepen your existing knowledge of C# and .NET, to have a solid grasp of the latest in C# and .NET OR to crack your next .NET Interview.

Click here to Explore the Table of Contents or Download Sample Chapters!

What Others Are Reading!
Was this article worth reading? Share it with fellow developers too. Thanks!
Share on LinkedIn
Share on Google+

Author
Suprotim Agarwal, MCSD, MCAD, MCDBA, MCSE, is the founder of DotNetCurry, DNC Magazine for Developers, SQLServerCurry and DevCurry. He has also authored a couple of books 51 Recipes using jQuery with ASP.NET Controls and The Absolutely Awesome jQuery CookBook.

Suprotim has received the prestigious Microsoft MVP award for Sixteen consecutive years. In a professional capacity, he is the CEO of A2Z Knowledge Visuals Pvt Ltd, a digital group that offers Digital Marketing and Branding services to businesses, both in a start-up and enterprise environment.

Get in touch with him on Twitter @suprotimagarwal or at LinkedIn



Page copy protected against web site content infringement 	by Copyscape




Feedback - Leave us some adulation, criticism and everything in between!
Comment posted by ravisankar on Thursday, April 3, 2008 8:27 AM
hi this is ravisankar. how to create the master page & sitemap plz explain that
Comment posted by ravisankar on Thursday, April 3, 2008 8:38 AM
hi this is ravisankar. how to create the master page & sitemap plz explain that
Comment posted by pavithra on Monday, June 16, 2008 10:51 PM
Hi,
If your .net application has 100 .aspx pages, from this
the  user should access only 20 .aspx pages then what changes will you make in your web.config file.
Comment posted by Shar on Wednesday, December 3, 2008 1:28 PM
Hi,

I am looking at some code where Javascript is used to validate the controls on the page and the error messages are retrieved from the AppSetting section of the web.config as in your blog.  Is this good practise? since javascript is client side and the error messages are in the web.config, is it hitting the server each time it retrieves the error string?  For localization, iss there a way to access the resource.resx instead via javascript instead?

Please let me know.

Thanks,

Shar
Comment posted by NS on Thursday, March 19, 2009 10:21 PM
Useless, even a beginner know this. i thought that it should be something tricky  
Comment posted by Raj on Monday, June 15, 2009 11:04 PM
Good example, but bad example.  Displaying and showing the web.config connection string?  I think it would have been better to show a general key.
Comment posted by Amit Habu on Thursday, July 23, 2009 7:32 AM
PLease can any one help me ,that how can i exceute my sql stmt via the given above connection
and achieve my records and dispaly it in browser
Comment posted by Deepak on Friday, November 27, 2009 5:09 AM
this code not working....
Comment posted by Nader H on Friday, January 15, 2010 2:00 PM
This code doesn't work. It doesn't shows values but actual text
Comment posted by petr on Wednesday, September 1, 2010 12:42 AM
this code not working....
Comment posted by Suprotim Agarwal on Thursday, September 2, 2010 12:51 AM
Code works just fine. What is the error you guys are getting?
Comment posted by Dennis on Tuesday, April 19, 2011 11:49 AM
I tried this code, and it doesn't work for me. I get this: ...error.html?aspxerrorpath=/testing2.aspx.

Comment posted by Sandeep on Friday, March 9, 2012 7:01 AM
This was the code i was searching.... It is working perfect... Thanks dude.....
Comment posted by Samir on Saturday, February 16, 2013 12:17 PM
show me a error in the building time.
could you help me please, are the something that i have to import ?
Comment posted by vijayalakshmi bangaru on Friday, October 18, 2013 5:33 AM
hi, i have used the same code in masterpage but its not working
pls help me how to fetch the value of a key in master page aspx