Manoj Singh
Manoj Singh

Reputation: 7707

Reading web.config value through javascript

I have web.config with the 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 java script.

I am using below code:

<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();" />

The error generated is:

Error 'System.Configuration.ConfigurationManager.AppSettings' is a 'property' but is used like a 'method' 

Upvotes: 3

Views: 17788

Answers (3)

Pradeep Iyer
Pradeep Iyer

Reputation: 11

Try this:

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

Please note that square brackets are used instead of normal brackets.

Upvotes: 1

Lee Kowalkowski
Lee Kowalkowski

Reputation: 11751

If it's a property (variable), you can't call it, like its a method (function). So don't you need:

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

...?

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

Upvotes: 0

Greg
Greg

Reputation: 321766

Edit: this doesn't answer your first issue, but still applies after you fix that. If vDirectory was something like "c:\new folder" you'd end up with a newline in t.

I'm not sure what language you're using but you want to run the string though addslashes() (or the equivalent in your language) before you print it out like that:

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

Or even better, JSON encode it if there's a function for that:

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

Upvotes: 1

Related Questions