Reputation: 16639
I want to read an app key from the web.config file via java script. The web.config key to be read
<appSettings>
<add key="Key1" value="value1" />
<appSettings>
I include the following inside my java script function.
function Evaluate() {
var key = '<%=ConfigurationManager.AppSettings["Key1"].ToString() %>';
alert(key);
}
However, I end up getting <%=ConfigurationManager.AppSettings["Key1"].ToString() %>
in the alert.
What am i missing?
Upvotes: 6
Views: 28020
Reputation: 820
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, not necessary to declare it.
on the web config:
</appSettings>
<add key="varName" value="1" />
</appSettings>
on the html page:
<script>
var varName= '@System.Configuration.ConfigurationManager.AppSettings["varName"]';
</script>
Upvotes: 5
Reputation: 643
The <%= =>
tag is only going to execute if it is within a .aspx
file. If you place it within a .js file, then it will just be like any other text. In order for your code to work, the javascript you posted would have to be embedded within the .aspx file.
Upvotes: 14