问题
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