Web.config file has configuration settings for ASP.NET web application. We will store application level settings and connection string value in it. In this snippet, we will see how to read values which are defined in appsettings and connectionStrings sections of web.config in JavaScript using ASP.NET.
In the following code, we will add values for appSettings and connectionStrings sections of web.coinfig file then from JavaScript we will try to read the values.
Web.config
<configuration>
<appSettings>
<add key="appKeyPI" value="3.15"/>
</appSettings>
<connectionStrings>
<add name="conStr" connectionString="Here is my connection string"/>
</connectionStrings>
</configuration>
ASPX Page
<head>
<script type="text/javascript">
var appKeyVal = '<%=ConfigurationSettings.AppSettings["appKeyPI"].ToString()%>';
var conStrVal = '<%=ConfigurationManager.ConnectionStrings["conStr"].ConnectionString %>';
alert("AppSettings value is: " + appKeyVal);
alert("Connection string value is: " + conStrVal);
</script>
</head>
output:
we will get two alter's with text as "AppSettings value is: 3.15" and "Connection string value is: Here is my connection string"
Basically in JavaScript, we tried to access the configuration settings values with server side code using <%= %>.