【问题标题】:How to access web.config key in JavaScript without using .aspx page?如何在不使用 .aspx 页面的情况下访问 JavaScript 中的 web.config 键?
【发布时间】:2017-07-31 16:55:38
【问题描述】:

我正在尝试访问 JavaScript 中的 web.config 键,但不想将代码添加到 .aspx 文件。这是我的代码:

Web.config 文件:

<add key="key" value="password"/>

JavaScript 文件:

var param = '<%= System.Configuration.ConfigurationManager.AppSettings["key"].ToString() %>';

对可能出了什么问题的想法?

谢谢!

【问题讨论】:

  • 是的,它在appSettings下。

标签: javascript c# asp.net .net web-applications


【解决方案1】:

您不能直接从js 文件访问web.config 或任何服务器端相关数据。您可以将要从 .aspx mester 页面上的客户端访问的所有数据分配给 window 对象,然后在 js 文件中访问它。

【讨论】:

    【解决方案2】:

    您的代码看起来不错,我就是这样做的,请参阅下面我的旧 web.forms 项目中的示例代码。

    <script type="text/javascript">
        var googleAnalyticsTrackingEnabled = '<%=ConfigurationManager.AppSettings["GoogleAnalyticsTrackingEnabled"].ToString() %>';
    
        if (googleAnalyticsTrackingEnabled.toLowerCase() == 'true') {
    
    // DO WHATEVER
    
        }
    
    </script>
    

    这是我的 web.config 中的关键值;

    <configuration>
    ...
    <appSettings>
      ...
      <!--Google Analytics settings-->
      <add key="GoogleAnalyticsTrackingEnabled" value="true" />
      ...
    </appSettings>
    ...
    </configuration>
    

    【讨论】:

      猜你喜欢
      • 2011-05-08
      • 2011-09-10
      • 1970-01-01
      • 2012-08-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-06-25
      • 1970-01-01
      相关资源
      最近更新 更多