Reputation: 61
I'm trying to access a web.config key in JavaScript, but don't want to add code to a .aspx file. Here's my code:
Web.config file:
<add key="key" value="password"/>
JavaScript file:
var param = '<%= System.Configuration.ConfigurationManager.AppSettings["key"].ToString() %>';
Thoughts on what could be going wrong?
Thanks!
Upvotes: 0
Views: 4798
Reputation: 1781
Your code looks good, that is exactly how I do it, see below my sample code from my old web.forms project.
<script type="text/javascript">
var googleAnalyticsTrackingEnabled = '<%=ConfigurationManager.AppSettings["GoogleAnalyticsTrackingEnabled"].ToString() %>';
if (googleAnalyticsTrackingEnabled.toLowerCase() == 'true') {
// DO WHATEVER
}
</script>
And this is the key value in my web.config for my case;
<configuration>
...
<appSettings>
...
<!--Google Analytics settings-->
<add key="GoogleAnalyticsTrackingEnabled" value="true" />
...
</appSettings>
...
</configuration>
Upvotes: 0
Reputation: 2614
You can not access web.config
or any serverside related data from js
files directly. You can assign all data you want to access from client on .aspx mester page to window
object and then access it in js
files.
Upvotes: 1