views:

735

answers:

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  A: 

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

David M
+2  A: 

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.?

Rob
Nice suggestion. Thanks for the help.
mdresser
Wouldn't this be better as a web service, asmx file?
Pete Michaud