0

I am using this tutorial to use a variable from web.config file into .NET . Now I want to use the exact same variable in JavaScript but according to my research the WebConfigurationManager variable is not available in HTML , any clue on how to do that ?

thanks

EDIT :

I tried to implement the suggested code in that way (just to make sure that it gives me the output I want):

var myJsVar = '<%= ConfigurationSettings.AppSettings["MyConfigValue"] %>';
alert(myJsVar);

the value of myJsVar come up as

<%= ConfigurationSettings.AppSettings["MyConfigValue"] %>

and when I do '<%= ConfigurationSettings.AppSettings["MyConfigValue"] %>'

and when I do alert(myJsVar.valueof()); instead I receive undefined as output

I also tried to use WebConfigurationManager instead of ConfigurationSettings and it goes through the same logic ...

2
  • What version of asp.net are you using? Commented Apr 15, 2013 at 17:47
  • I am new to all this ... can it be IIS7 ? Commented Apr 15, 2013 at 18:13

2 Answers 2

3

You can render the value to the aspx page, as an assignment to a JavaScript variable:

var myJsVar = '<%= ConfigurationSettings.AppSettings["MyConfigValue"] %>';

So this way, myJsVar will be initialized with MyConfigValue. Hope this helps.

EDIT

If you are using Razor you should use this way

var myJsVar = '@System.Configuration.ConfigurationManager.AppSettings["MyConfigValue"]';
Sign up to request clarification or add additional context in comments.

4 Comments

: thanks for your answer , I ran into some issues , can you please check the modifications to my question ?
are you sure that your page is an aspx page? or is an HTML page?
and I need this part in javascript
You are using Razor (that's an important difference). I've updated the post for using app settings using cshtml pages.
3

You just need to output the setting into the variable you want. Somewhere in your javascript you would put:

<script language="javascript">

    var mySetting = '<%=ConfigurationManager.AppSettings["mySetting"] %>';

</script>

Here the <%= is a Response.Write command that will output the string result to the markup rendered. From this point on the variable mySetting would be accessible to any components.

1 Comment

@JoelEtheron : thanks for your answer , I ran into some issues , can you please check the modifications to my question ?

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.