asp.net mvc c# javascript web.config

5
我想在JavaScript中从web.config的appsettings中检索“imagetype”。我该怎么做?
2个回答

8
您可以在页面标记中使用以下代码:
<script language="JavaScript" type="text/javascript">
var type = '<%= ConfigurationManager.AppSettings["imagetype"] %>';
</script>

1
请使用以下内容:
var value = System.Configuration.ConfigurationManager.AppSettings["imagetype"];

你可能会发现,为了使它正常工作,你需要添加一个对System.Configuration.dll的引用,如果你还没有这个引用的话。
创建一个新页面,在Page_Load中加入这一行,使其全部内容如下:
Response.Clear();
var value = System.Configuration.ConfigurationManager.AppSettings["imagetype"];
Response.Write(value);
Response.End();

现在您可以使用Javascript向页面发起AJAX调用,例如使用ExtJs,并将文本返回到您的javascript中。

或者,您可以将以下内容放入您的页面中:

<script language="javascript" type="text/javascript">
  var appSettingValue =  '<%=System.Configuration.ConfigurationManager.AppSettings["imagetype"]%>';

  // The variable "appSettingValue" will contain the string from your web.config
  alert(appSettingValue);
</script>

@ŁukaszW.pl,没错,但总体意思是一样的。由于今天很长,出于某种原因,我的眼睛在看到“asp.net”后就停止了阅读!=) - Rob

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接