在app.settings中传递javascript变量(客户端变量到服务器端)

时间:2022-02-18 03:57:56

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.

我想在javascript中读取配置文件,它是谷歌的键值。在这里,我要做的是传递一个javascript变量“keyvalue”,它是配置文件中的keyname,但是它不能工作。我只是想让js函数根据站点URL自动获取谷歌键,无论它是test还是dev,还是qa.abc.com,谁能帮我读一下value form配置文件。

  <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


1  

Web.Config

. config

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

In Aspx page take

在Aspx页面

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

In javascript

在javascript中

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

Now access in C#

现在在c#访问

  string key=hidkey.value;//Here u get web.config [Setting] value

#2


0  

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

不能像这样将服务器端和客户端混合在一起。这种功能需要使用AJAX

#1


1  

Web.Config

. config

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

In Aspx page take

在Aspx页面

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

In javascript

在javascript中

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

Now access in C#

现在在c#访问

  string key=hidkey.value;//Here u get web.config [Setting] value

#2


0  

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

不能像这样将服务器端和客户端混合在一起。这种功能需要使用AJAX