最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Reading web.config value through javascript - Stack Overflow

programmeradmin3浏览0评论

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' 

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' 
Share Improve this question edited Jun 26, 2009 at 13:46 Greg 322k54 gold badges376 silver badges337 bronze badges asked Jun 26, 2009 at 13:32 Manoj SinghManoj Singh 7,70734 gold badges122 silver badges201 bronze badges 3
  • That should work (although is not the most reended way to handle this). What does the alert show? an empty value? – Andrea Commented Jun 26, 2009 at 13:36
  • Its displaying error Error 'System.Configuration.ConfigurationManager.AppSettings' is a 'property' but is used like a 'method' – Manoj Singh Commented Jun 26, 2009 at 13:40
  • I deleted my answer Manoj because it isn't what you need. – cgp Commented Jun 26, 2009 at 13:43
Add a ment  | 

3 Answers 3

Reset to default 1

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()) %>;

Try this:

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

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

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./en-us/library/system.configuration.configurationmanager.appsettings.aspx

发布评论

评论列表(0)

  1. 暂无评论