Reading web.config from JavaScript

Is there a way that I can read configuration values ​​in web.config with javascript? Why do I want to do this?

I have a timer on my website that will display a modal dialog with a countdown timer (countdown for 2 minutes) if the user is inactive for 20 minutes. If the user does not respond, he logs him out of the system. If he does, he pings to the server (to maintain the session) and saves the session in standby mode

These 15 minutes are hardcoded in the js file. I would rather get it from a config file / other file than its hard coding in JS

here is a snippet of code

$.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 a separate js file. Doing <% =%> will produce the error "illegal XML character [Break on this error] inactivity: <% = ConfigurationManager.AppSettings [" Inaction "]%>;"

+28
javascript web-config
source share
9 answers

You can create your JavaScript from ASP.NET.

Then simply write the server-side settings to var defaults as follows:

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

EDIT:

If you want to save your JavaScript in static js files, you can still initialize your var defaults from the small <script> displayed by your ASP.NET application. Your settings will be global, as will AppSettings in web.config .

+50
source share

Pass them 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 }; }; </script> 

Repeat the same as yours.

+14
source share

Direct access to the configuration file from a separate static JS file is not possible. One way to do this is to add a java script to the .aspx page.

My web configuration:

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

My aspx page enter image description here

We set this variable globally to access this variable inside our static js file my js file home.js

 function ReadConfigSettings() { alert( test); } 

Thus, we can read the web configuration values ​​from JavaScript in this way.

+8
source share

After placing the values ​​in the configuration file on the page that you will use, enter the java script as follows: you will access the value in the java script as global, it is not necessary to declare it.

in web configuration:

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

on the html page:

 <script> var varName= '@System.Configuration.ConfigurationManager.AppSettings["varName"]'; </script> 
+6
source share

Usually you do not want web.config be directly accessible to clients, as it may contain some important information about your configuration (for example, credentials for the database you are using).

It is 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' } 
+4
source share

Make your JS file dynamic and run it using the server side script that can read your web.config

0
source share

For example, you have an appsettings variable defined as

 add key="ApplicationId" value="2" 

Since you cannot read this from the javascript file (since it is located on the server), you can define the variable in the main file. therefore, your master file will contain a java script variable.

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

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

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

 alert(appId); 
0
source share

I tried the following method and it worked for me on the .aspx page I am working with. What I noticed doesn’t work for me uses '[', instead I used '(', and it worked for me.

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

Using regular js, you can use this in your layout.htmlcs, at the beginning:

  @{ <script> sessionStorage.setItem("ProductionHostURL", '@System.Configuration.ConfigurationManager.AppSettings["ProductionHostURL"]'); </script> } <!DOCTYPE html> 

Then in your main js file layout.htmlcs you can use this method similar to this:

 var urlBaseProduction; var urlBaseDevelopment; $(document).ready(function () { configureHostEnvironment() .... } 

In this method, configure the variables for use in production and development, for example:

 function configureHostEnvironment(){ HOST = sessionStorage.getItem("ProductionHostURL") if (HOST.length <= 0) { alert("Host not configured correctly") } else { urlBaseProduction= host + '/api/'; urlBaseDevelopment= host + port + '/api/'; } } 

If you have a suggestion or improvement on this method, please comment.

0
source share

All Articles