从 JavaScript 读取 web.config

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1910095/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-22 21:51:48  来源:igfitidea点击:

Reading web.config from JavaScript

asp.netjavascriptweb-config

提问by ram

IS there any way that I can read config values in web.config with javascript ? Why would I want to do that ?

有什么方法可以使用 javascript 读取 web.config 中的配置值吗?我为什么要这样做?

I have a timer in my website which would pop up a modal dialog with a count down timer (count down for 2 minutes) if the user is inactive for 20 minutes. If the user does not respond, it logs him out. If he does, it pings to the server (to maintain the session) and keeps the session alive

我的网站上有一个计时器,如果用户处于非活动状态 20 分钟,它会弹出一个带有倒计时计时器(倒计时 2 分钟)的模式对话框。如果用户没有响应,它会将他注销。如果他这样做,它会ping服务器(以维持会话)并使会话保持活动状态

This 15 minutes is hardcoded in the js file. I would rather want to pick it up from a config file/some other file than having it hard coded into the JS

这 15 分钟硬编码在 js 文件中。我宁愿从配置文件/其他文件中提取它,也不愿将其硬编码到 JS 中

here is the code snippet

这是代码片段

$.fn.idleTimeout = function(options) {
        var defaults = {
                    //I would like to pick these values from some config file
            inactivity: 900000, //15 minutes 
            noconfirm: 120000, //2 minutes
            sessionAlive: 900000, //15 minutes
            click_reset: true,
            logout_url: '/Views/Pages/Timeout.aspx' 
        }

Any suggestions?

有什么建议?

Edit: This is in a separate js file. Doing <%=%> would give error "illegal XML character [Break on this error] inactivity: <%=ConfigurationManager.AppSettings["Inactivity"] %>;"

编辑:这是在一个单独的 js 文件中。执行 <%=%> 会给出错误“非法的 XML 字符 [Break on this error] inactivity: <%=ConfigurationManager.AppSettings["Inactivity"] %>;”

回答by Daniel Vassallo

You can generate your JavaScript from ASP.NET.

您可以从 ASP.NET 生成 JavaScript。

Then simply write the settings at the server-side to your var defaultslike this:

然后只需将服务器端的设置写成var defaults这样:

var defaults = {
    inactivity: <%=ConfigurationManager.AppSettings["Inactivity"] %>
}

EDIT:

编辑:

If you want to keep your JavaScript in static js files, you can still initialize your var defaultsfrom a small <script>rendered by your ASP.NET application. Your settings would be global, just like the AppSettingsin web.config.

如果您想将您的 JavaScript 保存在静态 js 文件中,您仍然可以var defaults<script>您的 ASP.NET 应用程序呈现的一个小文件中初始化您的 JavaScript 。您的设置将是全局的,就像AppSettingsin 一样web.config

回答by Jarrett Meyer

Pass them in as variables from the view.

从视图中将它们作为变量传递。

<script type="text/javascript">
   // needs to reside in your *.aspx file.
   $(function() {
       initPage(<%= Settings.Default.Inactivity %>, <%= Settings.Default.NoConfirm %>, <%= Settings.Default.LogoutUrl %>)
    });
    // Can reside in your *.aspx or in a *.js file.
    function initPage(inactivity, noconfirm, logoutUrl) {
        $.fn.idleTimeout = function(options) {
            inactivity: inactivity,
            noconfirm: noconfirm,
            logout_url: logoutUrl
        };
    };
</script>

Repeat for as many vars as you've got.

重复尽可能多的变量。

回答by Lijo

It's not possible for directly accessing the config file from separate static JS file. One way for doing this is to add the java script in the .aspx page.

无法从单独的静态 JS 文件直接访问配置文件。执行此操作的一种方法是在 .aspx 页面中添加 java 脚本。

My Web Config:

我的网络配置:

<appSettings>
   <add key="test" value="textBox"/>
</appSettings>

My aspx page enter image description here

我的aspx页面 在此处输入图片说明

We are globally setting this variable so we can access this variable inside our static js file my js file home.js

我们正在全局设置这个变量,所以我们可以在我们的静态 js 文件我的 js 文件home.js 中访问这个变量

function ReadConfigSettings()
{
   alert( test);
}

So we can read web config values from javascript by this way.

所以我们可以通过这种方式从 javascript 读取 web 配置值。

回答by Thiago Araújo

After to put the values on the config file, on the page that you will use the value put the java script this way bellow: You will access the value in the java script as a global, it is not necessary to declare it.

将值放入配置文件后,在您将使用该值的页面上,以如下方式放置 java 脚本:您将在 java 脚本中访问该值作为全局,不需要声明它。

on the web config:

在网络配置上:

 </appSettings>
    <add key="varName" value="1" />
  </appSettings>

on the html page:

在 html 页面上:

<script>
    var varName= '@System.Configuration.ConfigurationManager.AppSettings["varName"]';
</script>

回答by abahgat

Usually you don't want web.configto directly accessible to clients, since it may contain some important information about your configuration (for example, the credentials for the database you are using).

通常您不想web.config让客户端直接访问,因为它可能包含一些关于您的配置的重要信息(例如,您正在使用的数据库的凭据)。

It's better to make your JS file dynamic and add the appropriate parameters from your configuration. For example:

最好使您的 JS 文件动态化并从您的配置中添加适当的参数。例如:

$.fn.idleTimeout = function(options) {
    var defaults = {
                //I would like to pick these values from some config file
            inactivity: <%=ConfigurationManager.AppSettings["Inactivity"].ToString() %>, 
            noconfirm: <%=ConfigurationManager.AppSettings["NoConfirm"].ToString() %>, //2 minutes
            sessionAlive: <%=ConfigurationManager.AppSettings["SessionAlive"].ToString() %>, //15 minutes
            click_reset: true,
            logout_url: '/Views/Pages/Timeout.aspx' 
    }

回答by clarifier

I have tried out the following way and it worked for me in the .aspx page i am working with,the thing which i noticed not working for me is using the '[',instead i used '(' and it worked for me.

我尝试了以下方法,它在我正在使用的 .aspx 页面中对我有用,我注意到对我不起作用的是使用“[”,而是使用“(”,它对我有用。

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

回答by Cheranga

As an example, you have a appsettingsvariable defined as

例如,您有一个appsettings变量定义为

add key="ApplicationId" value="2"

Since from a javascript file you cannot read this (since this resides in the server), what you can do is define the variable in the master file. so your master file will contain the java script variable.

由于您无法从 javascript 文件中读取它(因为它位于服务器中),因此您可以做的是在主文件中定义变量。所以你的主文件将包含 java 脚本变量。

<% var appId= ConfigurationManager.AppSettings["ApplicationId"]; %>

Now from the java script file you can access this variable since this siteHost variable has a global scope in the java script.

现在您可以从 java 脚本文件访问这个变量,因为这个 siteHost 变量在 java 脚本中有一个全局范围。

So you can access this variable in your .jsfile as,

所以你可以在你的.js文件中访问这个变量,

alert(appId);

回答by Pete Duncanson

Make your JS file dynamic and have it timeout written out by a server-side script which can read your web.config

使您的 JS 文件动态化并使其超时由可以读取您的 web.config 的服务器端脚本写出