Click here to Skip to main content
15,884,059 members
Articles / Web Development / ASP.NET
Tip/Trick

Read Configuration Settings of Web.config using Javascript

Rate me:
Please Sign up or sign in to vote.
3.43/5 (6 votes)
30 Apr 2010CPOL1 min read 115.2K   3   10
Web.config is the main settings and configuration file for an ASP.NET web application. The file is an XML document that defines configuration information regarding the web application. The web.config file contains information that control module loading, security configuration, session state...
Web.config is the main settings and configuration file for an ASP.NET web application. The file is an XML document that defines configuration information regarding the web application. The web.config file contains information that control module loading, security configuration, session state configuration, and application language and compilation settings. Web.config files can also contain application specific items such as database connection strings.

The below method is used to access non-secure keys/values in the web.config don't use it to access secure sections as the ConnectionString for example, because the value will be rendered in the html of the page.


Example 1:
XML
<!-- This is an example Web.config file -->
<configuration>
    <appSettings>
            <add key="var1" value="SomeValue"/>
      </appSettings>
<configuration>


In this article, we will see how to read the configuration settings in the web.config using ‘javascript’.

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 entry to the appSettings section in the web.config between the <configuration> tag as shown in the example 1:
XML
<add key="var1" value="SomeValue"/>


Step 3: To read these entries using javascript, add the following script in the <head> tag of your Default.aspx page as shown below:

XML
<head runat="server">
    <title></title>
    <script type="text/javascript">
    function ReadConfigurationSettings()
    {
        var k = '<%=ConfigurationManager.AppSettings["var1"].ToString() %>'
        alert(k);
    }
    </script>
</head>


Step 4: Call this function on a button click and display the values of the configuration settings:
<input type="button" value="Get" onclick="ReadConfigurationSettings();" />



You can update this code to read any section in the web.config.
Now Run the application and click the button. The value of the key "var1" in the appSettings will be displayed in the alert window.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect
Lebanon Lebanon
I have more than 15 years of experience working with Microsoft technologies.
You can subscribe to my Newsletter to receive updates about the latest posts on ASP.NET Subscribe.

Comments and Discussions

 
QuestionJavascript means client side !!! Pin
Member 1227783822-Mar-19 0:04
Member 1227783822-Mar-19 0:04 
QuestionIdentifier expected Pin
manipriya62114-Feb-14 0:22
manipriya62114-Feb-14 0:22 
AnswerRe: Identifier expected Pin
Aravindba4-May-15 21:19
professionalAravindba4-May-15 21:19 
GeneralMy vote of 1 Pin
Member 1048480023-Dec-13 22:26
Member 1048480023-Dec-13 22:26 
Questionaccess config values in html page Pin
revanthkumar.ch15-May-12 23:37
revanthkumar.ch15-May-12 23:37 
GeneralReason for my vote of 1 No result Pin
deepak negi18-Jul-10 19:14
deepak negi18-Jul-10 19:14 
GeneralI think your title is wrong Pin
Xmen Real 1-May-10 18:44
professional Xmen Real 1-May-10 18:44 
GeneralRe: I think your title is wrong Pin
Jamil Hallal2-May-10 21:33
professionalJamil Hallal2-May-10 21:33 
GeneralRe: I think your title is wrong Pin
Xmen Real 2-May-10 23:15
professional Xmen Real 2-May-10 23:15 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.