Reading web.config from JavaScript

IS there any way that I can read config values in web.config with javascript ?
Why would I want to do that ?

I have a timer in my website which would pop up a modal dialog with a count down timer (count down for 2 minutes) if the user is inactive for 20 minutes. If the user does not respond, it logs him out. If he does, it pings to the server (to maintain the session) and keeps the session alive

This 15 minutes is hardcoded in the js file. I would rather want to pick it up from a config file/some other file than having it hard coded into the JS

here is the code snippet

$.fn.idleTimeout = function(options) {
        var defaults = {
                    //I would like to pick these values from some config file
            inactivity: 900000, //15 minutes 
            noconfirm: 120000, //2 minutes
            sessionAlive: 900000, //15 minutes
            click_reset: true,
            logout_url: '/Views/Pages/Timeout.aspx' 

Any suggestions?

Edit: This is in a separate js file. Doing <%=%> would give error “illegal XML character
[Break on this error] inactivity: <%=ConfigurationManager.AppSettings[“Inactivity”] %>;”


Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

You can generate your JavaScript from ASP.NET.

Then simply write the settings at the server-side to your var defaults like this:

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


If you want to keep your JavaScript in static js files, you can still initialize your var defaults from a small <script> rendered by your ASP.NET application. Your settings would be global, just like the AppSettings in web.config.

Method 2

Pass them in as variables from the view.

<script type="text/javascript">
   // needs to reside in your *.aspx file.
   $(function() {
       initPage(<%= Settings.Default.Inactivity %>, <%= Settings.Default.NoConfirm %>, <%= Settings.Default.LogoutUrl %>)
    // Can reside in your *.aspx or in a *.js file.
    function initPage(inactivity, noconfirm, logoutUrl) {
        $.fn.idleTimeout = function(options) {
            inactivity: inactivity,
            noconfirm: noconfirm,
            logout_url: logoutUrl

Repeat for as many vars as you’ve got.

Method 3

It’s not possible for directly accessing the config file from separate static JS file. One way for doing this is to add the java script in the .aspx page.

My Web Config:

   <add key="test" value="textBox"/>

My aspx page
enter image description here

We are globally setting this variable so we can access this variable inside our static js file my js file home.js

function ReadConfigSettings()
   alert( test);

So we can read web config values from javascript by this way.

Method 4

After to put the values on the config file, on the page that you will use the value put the java script this way bellow: You will access the value in the java script as a global, it is not necessary to declare it.

on the web config:

    <add key="varName" value="1" />

on the html page:

    var varName= '@System.Configuration.ConfigurationManager.AppSettings["varName"]';

Method 5

Usually you don’t want web.config to directly accessible to clients, since it may contain some important information about your configuration (for example, the credentials for the database you are using).

It’s better to make your JS file dynamic and add the appropriate parameters from your configuration. For example:

$.fn.idleTimeout = function(options) {
    var defaults = {
                //I would like to pick these values from some config file
            inactivity: <%=ConfigurationManager.AppSettings["Inactivity"].ToString() %>, 
            noconfirm: <%=ConfigurationManager.AppSettings["NoConfirm"].ToString() %>, //2 minutes
            sessionAlive: <%=ConfigurationManager.AppSettings["SessionAlive"].ToString() %>, //15 minutes
            click_reset: true,
            logout_url: '/Views/Pages/Timeout.aspx' 

Method 6

I have tried out the following way and it worked for me in the .aspx page i am working with,the thing which i noticed not working for me is using the ‘[‘,instead i used ‘(‘ and it worked for me.

var key = '<%=System.Configuration.ConfigurationManager.AppSettings("Environment").ToString() %>';

Method 7

Make your JS file dynamic and have it timeout written out by a server-side script which can read your web.config

Method 8

As an example, you have a appsettings variable defined as

add key="ApplicationId" value="2"

Since from a javascript file you cannot read this (since this resides in the server), what you can do is define the variable in the master file. so your master file will contain the java script variable.

<% var appId= ConfigurationManager.AppSettings["ApplicationId"]; %>

Now from the java script file you can access this variable since this siteHost variable has a global scope in the java script.

So you can access this variable in your .js file as,


Method 9

<add key="pageRedirect" value="http://localhost:1110/api/"/>
    <!--In layout.cshtml-->
    <a href="@Url.Action("Index", "CustodioExterno")" class="nav-link">
    <i class="far fa-circle nav-icon fx-xs"></i>
<p class="text-sm">Mantenedor</p>`enter code here`
<!--In a function-->
         function Redirigir(url) {
              window.location.href = url;

All methods was sourced from or, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Notify of

Inline Feedbacks
View all comments
Would love your thoughts, please comment.x