Read Configuration Settings of Web.config using Javascript
Posted by: Suprotim Agarwal ,
on 3/25/2008,
in
Category ASP.NET
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.
This article has been editorially reviewed by Suprotim Agarwal.
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!
Was this article worth reading? Share it with fellow developers too. Thanks!
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