【发布时间】:2017-03-14 13:17:10
【问题描述】:
我正在尝试在我的 .aspx 页面中使用 javascript 从 web.config 读取键值。 此页面包含在母版页中,脚本块添加到我的 aspx 页面的 ContentPlaceHolder 标记中,如下所示;这个问题之前在这里问过how to read values from web.config in javascript in aspx page,但没有明确的解决方案。有人建议返回服务器端,但我试图避免返回服务器以加速用户在页面上的工作......所以下面是我的代码:
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server">
<script type="text/javascript">
function CheckCountry() {
var country = '<%= System.Configuration.ConfigurationManager.AppSettings["LoginCountry"].ToString() %>';
alert(country);
}
window.onload = CheckCountry;
</script>
....
</asp:Content>
但每当我尝试运行我的项目时,我都会在 Visual Studio 2010 的“列表错误”框中收到此错误。
我也试过这个'<%=ConfigurationManager.AppSettings["LoginCountry"].ToString() %>' 但没有用,同样的错误被抛出......
可能是什么问题呢?如何解决这个问题并从我的 aspx 页面中的 web.config 获取国家/地区的值?
【问题讨论】:
-
嗯,当我尝试它时它工作了......你的
web.config<appSettings>节点是什么样的? -
@VDWWD 就是这样
-
看起来正确。如果您忽略错误会发生什么。或者从 javascript 中删除
'。有时编辑器不理解结合内联代码的 javascript。 -
@VDWWD 如果我忽略它,打开此页面时会抛出它;如果我删除 ' 怎么写?
-
可能是因为
在 <location allowOverride="true" inheritInChildApplications="true">标签内吗?喜欢这里:<location allowOverride="true" inheritInChildApplications="true"><appSettings> <add key="LoginCountry" value="LL"/> </appSettings></location>
标签: javascript asp.net web-config key-value