Reading web.config value through javascript

I have web.config with a given value:

<appSettings>
        <add key="vDirectory" value="fr" />
        <add key="BookingSummaryPage" value="/pli/forms/BookingSummary.aspx" />
</appSettings>

Now I want to read the value of "vDirectory" through a java script.

I am using the code below:

<script language="javascript" type="text/javascript">

function test()
{
var t='<%=ConfigurationManager.AppSettings("vDirectory").ToString() %>'
alert(t);
}
</script>

<input type="button" value="Click Me" onclick="test();" />

Error Created:

Error 'System.Configuration.ConfigurationManager.AppSettings' is a 'property' but is used like a 'method' 
+3
source share
3 answers

Edit: this does not answer your first problem, but still applies after you fix it. If vDirectory was something like "c: \ new folder", you would get a new line in t.

I'm not sure which language you are using, but you want to run the line though addslashes () (or the equivalent in your language) before printing it like this:

var t='<%=addslashes(ConfigurationManager.AppSettings("vDirectory").ToString()) %>';

Or even better, JSON encodes it if there is a function for this:

// Note no quotes as json_encode will add them
var t=<%=json_encode(ConfigurationManager.AppSettings("vDirectory").ToString()) %>;
+1
source

Try the following:

ConfigurationManager.AppSettings [ "vDirectory" ]. ToString()

, .

+1

If this property (variable), you cannot call it, for example, its method (function). So you do not need:

<%=ConfigurationManager.AppSettings.GetKey("vDirectory")%>

...

http://msdn.microsoft.com/en-us/library/system.configuration.configurationmanager.appsettings.aspx

0
source

Source: https://habr.com/ru/post/1711344/


All Articles