0

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!

2 Answers 2

7

You can use this

<script>
    var varName= '@System.Configuration.ConfigurationManager.AppSettings["KeyName"]';
</script>
Sign up to request clarification or add additional context in comments.

2 Comments

This should have been the accepted answer.... anyway, thank you, it helped me a lot
Yep. And for a connection string: var varName = '@System.Configuration.ConfigurationManager.ConnectionStrings["KeyName"]';
0

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).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.