2

Is there an easy way to read settings from a (asp.net) web.config file using javascript within a html page?

BTW I have found examples of passing config settings into javascript blocks within aspx pages e.g.

<script type="text/javascript">
function ReadConfigSettings(){   
   var wibble = '<%=ConfigurationManager.ConnectionStrings["MyConnString"].ConnectionString %>'   }
</script>

but this is NOT what I'm trying to do. I need to read the settings from within a stand-alone html page.

1
  • 2
    If Javascript could read your connection string, you would have a serious security problem. Commented Apr 1, 2009 at 14:33

3 Answers 3

4

If the HTML/Javascript is running from a Virtual Directory in IIS then there's no direct way to access the .config file in Javascript as the asp.net handlers prevent ".config" files from being served. So, no loading the file into an IFrame/Ajax request.

One option is an .aspx file on the server which returns the applicable value from the .config file, so:

http://myhost/mydirectiory/GetConfigurationValue.aspx?key=MyConnString

called from Javascript would return XML of "connectionStringGoesHere" which can then be used.?

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

1 Comment

Wouldn't this be better as a web service, asmx file?
1

You can't. The config file is server side and not accessible (deliberately and quite rightly) from the client side.

Comments

0

An old question but another suggestion though it might not fit with your requirement for access using a pure HTML page...

However I am trying to do something similar Perhaps something like in the main page

<script type="text/javascript">
 function ReadConfigSettings(){   
 Window.wibble = '<%=ConfigurationManager.ConnectionStrings["MyConnString"].ConnectionString %>'           
}
</script>

this leaves Window.wibble a global variable accessable from elsewhere.

Variations on this include putting it inside a hidden input div and accessing it via jQuery / ElementById elsewhere... Something I do like such

<input id="connString" type="hidden" value="@Mvc.Utils.GetConnectionString()" />

Essentially just using the Server side to add the values into the html and then loading up the values in the client side later.

However since you require pure HTML and its a connection string you are passing, this probably fails for security etc reasons but it works for reading simple web config values.

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.