effeffe
effeffe

Reputation: 99

MVC4 read a AppKey value from web.config via javascript

I've looked at some post concerning the argument but I'm still missing something

because I can't read the key (in the appSettings tag of Web.config)

 <add key="ROOT_URL_SERVER" value="hello" />

with this javascript in this view (MVC4)

<input type="text" value="@ViewBag.prova" />

<script type="text/javascript">

        var t = '<%=ConfigurationManager.AppSettings["ROOT_URL_SERVER"].ToString()%>';
        var type = '<%= ConfigurationManager.AppSettings["ROOT_URL_SERVER"] %>';
        var appSettingValue = '<%=System.Configuration.ConfigurationManager.AppSettings["ROOT_URL_SERVER"]%>';

        alert(t);
        alert(type);
        alert(appSettingValue);

    </script>

even if it works when i pass the value by the viewbag with the code

{
            string test = ConfigurationManager.AppSettings["ROOT_URL_SERVER"].ToString();
            ViewBag.prova = test;

            return View();
        }

Can you help me fix this problem? Thank you!

Upvotes: 0

Views: 8984

Answers (2)

Niang Moore
Niang Moore

Reputation: 506

You can use this

<script>
    var varName= '@System.Configuration.ConfigurationManager.AppSettings["KeyName"]';
</script>

Upvotes: 7

Roy Dictus
Roy Dictus

Reputation: 33139

Client-side JavaScript can NOT read your config files. The only thing you can do is generate JavaScript code on the server that contains the value embedded, as you do in the example above (second code block).

Upvotes: 0

Related Questions