Monday, June 15, 2015

Reading a value from the Web.config file of an ASP .NET Web Application using Javascript

To read a value from the web.config file of an ASP .NET Web Application using javascript, you can do the following:

  1. In your web.config file, add a new <appSettings> with the key and value that you wish for the javascript code to obtain.  In this example, here is the new <appSettings> node, I'm going to reference:

    <appSettings>
         <add key="Environment" value="TEST" />
    </appSettings>


  2. In the HTML code of my default.aspx page, I'm going to add the following javascript function that's going to reference the new <appSettings> node that I just added:

    function getEnvironment() { 
         var environment = '<%= ConfigurationManager.AppSettings["Environment"].ToString() %>';
         alert(environment);
    }
All you have to do is call this function at an appropriate location in your code and it will obtain the value of the respective <appSettings> node.  Naturally, you'll want to swap out the alert call with some valid code, but the idea of this example is to get you rolling.

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.