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
4 Answers
Reset to default 4Try 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#.