Asked by:
Reading a AppKey value from web.config in clientside js

Question
-
User396067491 posted
Hi,
I don't know why this isn't working, everything I've read says it should. I have created a appkey variable in the web.config file and i'm now trying to read it in an aspx page within a javascript variable.
The setup is below:
web.config:
<appSettings file="">
<add key="webAccount" value="12345678" />
My javascript variable in the aspx page:
<script type="text/javascript"> var setWebAccountNumber = '<%=ConfigurationSettings.AppSettings["webAccount"].ToString() %>';
The error I get is shown below:
Compilation Error
Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.
Compiler Error Message: BC30203: Identifier expected.
Source Error:Line 34: <!-- Begin Header --> Line 35: <script type="text/javascript"> Line 36: var setWebAccountNumber = '<%=ConfigurationSettings.AppSettings["webAccount"].ToString() %>';
Wednesday, August 15, 2012 11:51 AM
All replies
-
User1839833660 posted
refer http://csharpdotnetfreak.blogspot.com/2012/07/read-webconfig-using-javascript-aspnet.html
Thursday, August 16, 2012 3:03 AM -
User1954304945 posted
Hi, there is no error with your codes, I tested on my computer and found they worked. I suggest commenting the error line and try to print
ConfigurationSettings.AppSettings["webAccount"].ToString()
with behind codes to check whether the value can be got properly.
Default.aspx:
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> <script type="text/javascript"> //var setWebAccountNumber = '<%=ConfigurationSettings.AppSettings["webAccount"].ToString() %>'; //alert(setWebAccountNumber); </script> </head> <body> <form id="form1" runat="server"> <div> </div> </form> </body> </html>
Default.aspx.cs:
protected void Page_Load(object sender, EventArgs e) { string val = ConfigurationSettings.AppSettings["webAccount"].ToString(); Response.Write(val); }
Thursday, August 16, 2012 10:45 PM -
User396067491 posted
thanks for the responses.
I did was you suggestioned (I can't believe I never thought of that) and tested the values from the code behind. It works fine. I just seems that the errors are thrown when I put the same code values in the javascript of the header. I can't figure out why, it should work.
Friday, August 17, 2012 1:18 PM -
User-1675424275 posted
Hello ,
any One Please help me ragarding , how can we access our Web.config Variable Inside a Dynamic .Js File ,that .Js File will be accesed By the User Directly Like
http://localhost/js/dynamictest.js (Include File).
thanks®ards,
Nigam Patta
Tuesday, April 15, 2014 3:27 AM