how to read values from web.config in javascript in aspx page

梦想的初衷 提交于 2019-12-11 00:14:00

问题


I have a javascript code in the aspx file.

In my script function, I am trying to read the 'SiteRootURL' value from the web.config as:

var k = '<%=ConfigurationManager.AppSettings["SiteRootURL"].ToString() %>'
     alert(k);

This is not working. When I add above code, I get blue squirkly lines at the top of my aspx page - <%@Page...%> which says - Identifier Expected'

When I remove the above line, the code runs fine. Please help me. What am I doing wrong? How can I read the value form the web.config in javascript.


回答1:


You can make a function on your code behind file like this:

protected static string GetAppSettingsValue()
{
    return ConfigurationManager.AppSettings("SiteRootURL").ToString();
}

And then call it on your javascript like so

<script type="text/javascript">
    alert('<%=GetAppSettingsValue%>');
</script>


来源:https://stackoverflow.com/questions/17354471/how-to-read-values-from-web-config-in-javascript-in-aspx-page

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!