Click here to Skip to main content
15,887,746 members
Please Sign up or sign in to vote.
1.44/5 (2 votes)
How to call System.Configuration.ConfigurationManager.AppSettings["x"] in to javascript file ?

What I have tried:

Tried with config manager and other parameters not succeed.
Posted
Updated 4-Jul-17 1:19am

1 solution

You can't call server-side code directly, you'll need your server code to render a javascript variable that has the data you need and then use that

On your aspx page (or view if you're using MVC)

<script>
    var mySetting = '<%=System.Configuration.ConfigurationManager.AppSettings["MySetting"]%>';
</script>
<script src="/scripts/test.js"></script>


test.js

$(document).ready(function () {
    if (typeof (mySetting) == "undefined"){
        // if you need to check if the variable has been defined you can do this
        return;
    }

    // even though mySetting is defined on your aspx page you can still use it in your js files
    // as long as they are included after the variable is defined
    alert(mySetting);
});
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900