首页 > 技术文章 > web.config设置和取值

mzyj 2015-06-10 23:18 原文

博客园中有一篇文章对web.config的结构做了很详细的介绍,原文见 http://www.cnblogs.com/gaoweipeng/archive/2009/05/17/1458762.html。

我只介绍一下对该文件中的数据的取用。

假设在web.config中有如下配置:

1 <appSettings>
2     <add key="AKWebservice" value="http://58.252.73.14:5236/pms_service/rest/" />
3 </appSettings>

假如在类AKParkingHelper.cs中需要使用上述内容的value值,可以按照下面的方式取值:

1 NameValueCollection nc = (NameValueCollection)ConfigurationSettings.GetConfig("appSettings");
2 string akw = nc["AKWebservice"];

akw最终的取值即是字符串"http://58.252.73.14:5236/pms_service/rest/"。

需要添加的引用如下:

NameValueCollection 的引用:using System.Collections.Specialized;
ConfigurationSettings 的引用:using System.Configuration;

 

推荐阅读