0

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!

1
  • Yep, it's under appSettings. Commented Jul 31, 2017 at 18:00

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.