MVC4 通过 javascript 从 web.config 读取 AppKey 值

eff*_*ffe 0 javascript web-config asp.net-mvc-4

我看过一些关于这个论点的帖子,但我仍然遗漏了一些东西

因为我无法读取密钥(在 Web.config 的 appSettings 标签中)

 <add key="ROOT_URL_SERVER" value="hello" />
Run Code Online (Sandbox Code Playgroud)

在此视图中使用此 javascript (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>
Run Code Online (Sandbox Code Playgroud)

即使当我用代码通过视图包传递值时它也能工作

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

            return View();
        }
Run Code Online (Sandbox Code Playgroud)

你能帮我解决这个问题吗?谢谢!

Nia*_*ore 5

你可以用这个

<script>
    var varName= '@System.Configuration.ConfigurationManager.AppSettings["KeyName"]';
</script>
Run Code Online (Sandbox Code Playgroud)