1

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 ?

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... Commented Oct 21, 2011 at 8:04
  • if you are using .net 4.0 you can use HttpUtility.JavaScriptStringEncode() Commented Oct 22, 2011 at 10:00

4 Answers 4

4

Try this

ConfigurationManager.AppSettings["RootPath"].ToString().Replace(@"\", @"\\")
Sign up to request clarification or add additional context in comments.

1 Comment

And then hope there isn't a ' in the file name :-) You should escape the ' and the " (both of them just to be sure)
0
var v1 = '<%= ConfigurationManager.AppSettings["RootPath"].Replace(@"\",@"\\") %>'

ToString() is excess

Comments

0

if you adding this

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

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

Comments

0

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

1 Comment

You need to do the addslashes bit in your server-side code not javascript

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.