Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
164 views
in Technique[技术] by (71.8m points)

asp.net - 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"] %>;"

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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"] %>
}

EDIT:

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...