0

I want to read the config file in javascript that is the google key value. here what i am trying to do is passing a javascript variable"keyvalue" that is the keyname in config file but it does not work. i just want it so that the js function automatically picks up the google key according to site URL whether it may be test or dev or qa.abc.com can anyone help me what should i do to read the value form config file.

  <script type="text/javascript">

function ReadConfigSettings()
{ 

var url="test.abc.com"; //window.location.href

var patharray= new Array();

patharray =url.split('.');

var first = patharray[0];

 var keyvalue="GoogleKey_"+ first;

 var key='<%=ConfigurationManager.AppSettings[keyvalue]%>';

 alert(key);
}

 </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>

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

    </div>
    </form>
</body>

my config file

<appSettings>
    <add key="GoogleKey_dev" value="ABQIAAAAJ4psDZ8kCtN062-LDcwiXhQ85a215fgrIsfghh547457h1ETJRYlwaBGrrytgytg56g7Mx4QFQ"/>
    <add key="GoogleKey_qa" value="ABQIAAAACoUjxmFCsPtytryhtyty547547Ryt5gVV28BYSHIaU0BRwPyLrf_gf546Jd_5qxcNZ-_b7WZw"/>
    <add key="GoogleKey_test" value="AIzaSyvbghgfyh54654650x2SlGb33KrTtIBc"/>
2
  • 1
    Read Configuration Settings of Web.config using Javascript Commented Aug 27, 2012 at 12:13
  • thanks for your answer but i want to pass a js variable in ConfigurationManager.AppSettings["Setting"] so simply i want is ConfigurationManager.AppSettings["my js variable"] and my js variable holds my key name. Commented Aug 28, 2012 at 4:30

2 Answers 2

1

Web.Config

</configuration>
    <appSettings>
        <add key="Setting" value="Value"/>
    <appSettings>
</configuration>

In Aspx page take

<asp:HiddenField runat='server' id='hidkey' />

In javascript

 document.getElementById('<%=hidkey.ClientID %>').value = '<%=System.Configuration.ConfigurationManager.AppSettings["Setting"] %>';

Now access in C#

  string key=hidkey.value;//Here u get web.config [Setting] value
Sign up to request clarification or add additional context in comments.

4 Comments

thanks for your answer but i want to pass a js variable in ConfigurationManager.AppSettings["Setting"] so simply i want is ConfigurationManager.AppSettings["my js variable"] and my js variable holds my key name.
not possible...because AppSettings["Setting"] extract from web.config file.
yes as you can see above in js i wrote var keyvalue="GoogleKey_"+ first; so i want to pass this variable in AppSettings[] so that it automatically picks up the value from webconfig as per the parameter such as GoogleKey_test or GoogleKey_dev etc
then take it in hiddenfield and acess on code behind(server side) its simple
0

You Can't Mix server side with client side, like this. you need to use AJAX for this kind of functionality

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.