Click here to Skip to main content
Click here to Skip to main content

Tagged as

Read Configuration Settings of Web.config using Javascript

, 30 Apr 2010 CPOL
Rate this:
Please Sign up or sign in to vote.
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:
<!-- 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:
<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:
 
<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)

Share

About the Author

Jamil Hallal
Technical Lead -
Lebanon Lebanon
I have 7+ years of experience working with Microsoft technologies. I have earned my Microsoft Certified Professional Developer. I'm a highly motivated self-starter with an aptitude for learning new skills quickly.

Comments and Discussions

 
QuestionIdentifier expected Pinmembermanipriya62114-Feb-14 1:22 
QuestionMessage Automatically Removed PinprofessionalRavi.Kumar02127-Dec-13 1:51 
GeneralMy vote of 1 PinmemberMember 1048480023-Dec-13 23:26 
Questionaccess config values in html page Pinmemberrevanthkumar.ch16-May-12 0:37 
GeneralReason for my vote of 1 No result Pinmemberdeepak negi18-Jul-10 20:14 
GeneralI think your title is wrong PinmemberXmen W.K.1-May-10 19:44 
GeneralRe: I think your title is wrong PinmemberJamil Hallal2-May-10 22:33 
GeneralRe: I think your title is wrong PinmemberXmen W.K.3-May-10 0:15 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.141220.1 | Last Updated 30 Apr 2010
Article Copyright 2010 by Jamil Hallal
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid