Reading web.config file using JavaScript

Suppose we have followings in our web.config file:

<appSettings>
<
add key="serverip" value="###.##.#.##"/>
</appSettings>
  <
connectionStrings>
    <
add name="conn" connectionString="Server=###.##.#.##;User=root;Password=12345;Database=test;"/>
  </connectionStrings>

Then using following set of steps we can read our config file.

Step 1:- Add the following piece of code in the head section of .aspx page.

<script type="text/javascript">
function ReadConfigFile()
{
    
var conStr = '<%=ConfigurationManager.ConnectionStrings["conn"].ConnectionString %>'
    alert(conStr);
    
var appStr = '<%=ConfigurationManager.AppSettings["serverip"].ToString() %>'
    alert(appStr);
}
</script>

Step-2: Now take a button and call the above function on its OnClientClick event.

<asp:Button ID="Button1" runat="server" Text="display webconfig" OnClientClick="ReadConfigFile()" />

Ebook Download
View all
Learn
View all