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

c# - Web.config value in javascript - Stack Overflow

programmeradmin2浏览0评论

i want to acess the web config value in javascript

config entry:

<add key ="RootPath" value ="C:\Test" />

javascript code:

  var v1 = '<%=ConfigurationManager.AppSettings["RootPath"].ToString() %>'

The output that i am getting is

 C:Test

but what i want is C:\Test

Any idea how to acheive this ?

i want to acess the web config value in javascript

config entry:

<add key ="RootPath" value ="C:\Test" />

javascript code:

  var v1 = '<%=ConfigurationManager.AppSettings["RootPath"].ToString() %>'

The output that i am getting is

 C:Test

but what i want is C:\Test

Any idea how to acheive this ?

Share Improve this question edited Oct 21, 2011 at 9:26 Falcon 6501 gold badge9 silver badges24 bronze badges asked Oct 21, 2011 at 7:59 user339160user339160 2
  • 1 as side note: I would really not call directly the ConfigurationManager from JavaScript. you could have that value rendered in an hidden field or worst case in a public static method of the page you access the ConfigurationManager and from JavaScript you call the public method, it starts getting nasty having client code reading config file directly, in my opinion... – Davide Piras Commented Oct 21, 2011 at 8:04
  • if you are using 4.0 you can use HttpUtility.JavaScriptStringEncode() – deostroll Commented Oct 22, 2011 at 10:00
Add a ment  | 

4 Answers 4

Reset to default 4

Try this

ConfigurationManager.AppSettings["RootPath"].ToString().Replace(@"\", @"\\")
var v1 = '<%= ConfigurationManager.AppSettings["RootPath"].Replace(@"\",@"\\") %>'

ToString() is excess

if you adding this

<add key ="RootPath" value ="C:\\Test" />

then you will retrive like "C:\Test". Its behavior of .

some thing like this

var defaults = {inactivity: <%=ConfigurationManager.AppSettings["Inactivity"] %> } 

Refer to the Read Configuration settings from javascript. If you are getting the config value like

C:\Test

change the config entry to C:\\Test and in C# especially in paths the \\ will be automatically converted in to \ because slash will be escaped by using escape sequence, because anything that begins with a backslash ('\') is termed as escape sequence in C#.

发布评论

评论列表(0)

  1. 暂无评论