refer : http://www.cnblogs.com/fish-li/archive/2011/12/18/2292037.html

上面这篇写很好了.

在做项目时,我们经常会遇到一些资料,我们不知道该把它们安置在何处.

比如公司资料

1.放在数据库 (资料不经常修改,没必要这么大费周章吧).

2.放在代码里头 (也不是说绝对不会改丫,写在代码里变成dll后不容易修改了耶 /.\)

大部分人都会把这样一群资料统统塞进 webConfig appSettings 里头

数据少的话,其实也没什么,只是一但数据多起来,我们就得分类来装置了。

这里我会教大家如果在 webConfig 中写自己的 Element 来不存资料,而不完全的塞在 appSettings 里头

从 appSettings 或者资料

<appSettings>
<add key="stringData" value="value" />
</appSettings> string value = ConfigurationManager.AppSettings["stringData"].ToString();

自定义一个 object

<configuration>
<configSections>
<section name="objectSection" type="Project.Config.objectSection" /> <!--要在 configSections 内注册哦-->
</configSections>  
<objectSection
stringData="value"
intData=""
/>
</configuration> //对应的 Class
//每一个属性一定要用 getter 来获取
public class objectSection : ConfigurationSection
{
[ConfigurationProperty("stringData", IsRequired = true)]
public string stringData
{
get { return this["stringData"].ToString(); }
} [ConfigurationProperty("intData", IsRequired = true)]
public int intData
{
get { return Convert.ToInt32(this["intData"]); } //这里可以做类型强转等等
}
}

嵌套 object

<configuration>
<configSections>
<section name="objectASection" type="Project.Config.objectASection" />
</configSections>
<objectASection>
<objectBSection
stringData="value"
intData=""
/>
</objectASection>
</configuration> public class objectASection : ConfigurationSection
{
[ConfigurationProperty("objectBSection", IsRequired = true)]
public objectBSection objectBSection
{
get { return (objectBSection)this["objectBSection"]; }
}
}
public class objectBSection : ConfigurationElement //子层继承 Element
{
[ConfigurationProperty("stringData", IsRequired = true)]
public string stringData
{
get { return this["stringData"].ToString(); } //这里可以做类型强转等等
} [ConfigurationProperty("intData", IsRequired = true)]
public int intData
{
get { return Convert.ToInt32(this["intData"]); } //这里可以做类型强转等等
}
}

嵌套 array + object + property 综合版

<configuration>
<configSections>
<section name="objectASection" type="Project.Config.objectASection" />
</configSections>
<objectASection stringData="value">
<objectBSection
stringData="value"
intData=""
/>
<objectCs>
<objectC Id="" stringData="x" />
<objectC Id="" stringData="y" />
<objectC Id="" stringData="z" />
</objectCs>
</objectASection>
</configuration> public class objectASection : ConfigurationSection
{
//normal value
[ConfigurationProperty("stringData", IsRequired = true)]
public string stringData
{
get { return this["stringData"].ToString(); } //这里可以做类型强转等等
} //object value
[ConfigurationProperty("objectBSection", IsRequired = true)]
public objectBSection objectBSection
{
get { return (objectBSection)this["objectBSection"]; }
} //array value
[ConfigurationProperty("objectCs", IsRequired = true)]
public objectCs objectCs
{
get { return (objectCs)this["objectCs"]; }
}
} [ConfigurationCollection(typeof(objectC), AddItemName = "objectC", CollectionType = ConfigurationElementCollectionType.BasicMap)]
public class objectCs : ConfigurationElementCollection
{
protected override ConfigurationElement CreateNewElement()
{
return new objectC();
} protected override object GetElementKey(ConfigurationElement element)
{
if (element == null) throw new ArgumentNullException("element");
return ((objectC)element).Id;
}
} public class objectC : ConfigurationElement
{
[ConfigurationProperty("Id", IsRequired = true, IsKey = true)]
public int Id
{
get { return Convert.ToInt32(this["Id"]); }
} [ConfigurationProperty("stringData", IsRequired = true)]
public string stringData
{
get { return this["stringData"].ToString(); }
}
} public class objectBSection : ConfigurationElement //子层继承 Element
{
[ConfigurationProperty("stringData", IsRequired = true)]
public string stringData
{
get { return this["stringData"].ToString(); } //这里可以做类型强转等等
} [ConfigurationProperty("intData", IsRequired = true)]
public int intData
{
get { return Convert.ToInt32(this["intData"]); } //这里可以做类型强转等等
}
} objectASection objectASection = (objectASection)ConfigurationManager.GetSection("objectASection");
string stringData = objectASection.stringData;
stringData = objectASection.objectBSection.stringData; //value
int intData = objectASection.objectBSection.intData; //
List<objectC> objectCs = objectASection.objectCs.Cast<objectC>().ToList();
foreach (objectC objectC in objectCs)
{
int Id = objectC.Id;
stringData = objectC.stringData;
}

还有一种就是类似 appSettings 那样本身就直接是 array 的情况

<configuration>
<configSections>
<section name="BusinessConfig" type="Project.Config.KeyValueConfig" /> <!--可以用同一个class-->
<section name="AbcConfig" type="Project.Config.KeyValueConfig" />
</configSections> <BusinessConfig>
<add key="aa" value=""></add>
<add key="bb" value=""></add>
</BusinessConfig>
<AbcConfig>
<add key="ha" value="a"></add>
<add key="ta" value="b"></add>
</AbcConfig>
</configuration>
namespace Project.Config
{
public class KeyValueConfig : ConfigurationSection
{
private static readonly ConfigurationProperty s_property = new ConfigurationProperty(string.Empty, typeof(MyKeyValueCollection), null, ConfigurationPropertyOptions.IsDefaultCollection);
[ConfigurationProperty("", Options = ConfigurationPropertyOptions.IsDefaultCollection)]
public MyKeyValueCollection KeyValues
{
get { return (MyKeyValueCollection)base[s_property]; }
} }
[ConfigurationCollection(typeof(MyKeyValueSetting))]
public class MyKeyValueCollection : ConfigurationElementCollection
{
public MyKeyValueCollection() : base(StringComparer.OrdinalIgnoreCase) { }
new public MyKeyValueSetting this[string name]
{
get { return (MyKeyValueSetting)base.BaseGet(name); }
} protected override ConfigurationElement CreateNewElement()
{
return new MyKeyValueSetting();
}
protected override object GetElementKey(ConfigurationElement element)
{
return ((MyKeyValueSetting)element).Key;
}
}
public class MyKeyValueSetting : ConfigurationElement
{
[ConfigurationProperty("key", IsRequired = true)]
public string Key
{
get { return this["key"].ToString(); }
set { this["key"] = value; }
} [ConfigurationProperty("value", IsRequired = true)]
public string Value
{
get { return this["value"].ToString(); }
set { this["value"] = value; }
}
}
}
KeyValueConfig mySection = (KeyValueConfig)ConfigurationManager.GetSection("BusinessConfig");
string data = string.Join("\r\n",
(from kv in mySection.KeyValues.Cast<MyKeyValueSetting>()
let s = string.Format("{0}={1}", kv.Key, kv.Value)
select s).ToArray());

ASP.NET Web.Config 读资料 (学习笔记)的更多相关文章

  1. ASP.NET Web.config学习

    花了点时间整理了一下ASP.NET Web.config配置文件的基本使用方法.很适合新手参看,由于Web.config在使用很灵活,可以自定义一些节点.所以这里只介绍一些比较常用的节点. <? ...

  2. ASP.NET web.config中数据库连接字符串connectionStrings节的配置方法

    ASP.NET web.config中数据库连接字符串connectionStrings节的配置方法 第一种情况,本地开发时,使用本地数据库,如下面的代码 <connectionStrings& ...

  3. Asp.net Web.Config - 配置元素 caching

    Asp.net Web.Config - 配置元素 caching 记得之前在写缓存DEMO的时候,好像配置过这个元素,好像这个元素还有点常用. 一.caching元素列表   元素 说明 cache ...

  4. 【笔记目录2】【jessetalk 】ASP.NET Core快速入门_学习笔记汇总

    当前标签: ASP.NET Core快速入门 共2页: 上一页 1 2  任务27:Middleware管道介绍 GASA 2019-02-12 20:07 阅读:15 评论:0 任务26:dotne ...

  5. asp.net web.config的学习笔记

    原文地址:http://www.cnblogs.com/Bulid-For-NET/archive/2013/01/11/2856632.html 一直都对web.config不太清楚.这几天趁着项目 ...

  6. ASP.NET Web.config文件的配置(Configuration API)

    本次我们讨论主要聚焦在以下Web.config配置文件的设置值的读取. 1.<connectionString />连接字符串的读取. 2.<appSettings />应用程 ...

  7. ASP.NET Web.config

    分析: .NET Web 应用程序的配置信息(如最常用的设置ASP.Net Web 应用程序的身份验证方式),它可以出现在应用程序的每一个目录中.当你通过VB.NET新 建 一个Web应用程序后,默认 ...

  8. 【Pro ASP.NET MVC 3 Framework】.学习笔记.5.SportsStore一个真实的程序

    我们要建造的程序不是一个浅显的例子.我们要创建一个坚固的,现实的程序,坚持使它成为最佳实践.与Web Form中拖控件不同.一开始投入MVC程序付出利息,它给我们可维护的,可扩展的,有单元测试卓越支持 ...

  9. ASP.NET web.config中的连接字符串

    在ASP.NET的web.config中,可以用两种方式来写连接字符串的配置. <configuration> <appSettings> <add key=" ...

随机推荐

  1. MVC4 Razor视图下使用iframe加载RDLC报表

    MVC视图下默认是不支持服务器端控件的,所以,为了能够通过report viewer控件加载报表,需要在MVC视图添加嵌入的页面. 起初在stackoverflow上找到一个解决方案,见这里.不过这里 ...

  2. vSphere文档中心

    http://pubs.vmware.com/vsphere-51/index.jsp#com.vmware.vsphere.install.doc/GUID-7C9A1E23-7FCD-4295-9 ...

  3. win7-64bit 下oracle11g plsql 的正确安装

    本人在PC机上安装了Oracle 11g 版本号的数据库服务,通过PL/SQL连接数据库时总是无法连接,位的oracle客户端工具instantclient进行转换,另外一种plsql的不能安装到文件 ...

  4. Android 监控网络状态

    public static boolean isNetworkAvailable(Context context) { ConnectivityManager connectivity = (Conn ...

  5. POJ 1250 Tanning Salon

    Tanning Salon Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 6822   Accepted: 3686 Des ...

  6. Creating Lists and Cards 创建列表和卡片

    To create complex lists and cards with material design styles in your apps, you can use the Recycler ...

  7. String对象之间的比较

    public class StringTest { @Test public void test01() { int a = 50; // 基本数据类型比较的是值 int b = 50; System ...

  8. 安装 vs2005, vs2008 报错

    最近重新装了系统之后,在安装 vs2005, vs2008 到如下类似的错误,苦苦两天没有解决.不要问为什么是 vs2005,vs2008, 因为原有的项目就是老版本. 无意间在网上看到一句话,大意是 ...

  9. 图解JavaScript知识点

  10. 字符串转化为json的三种方法

    1,eval方式解析,恐怕这是最早的解析方式了.如下: function strToJson(str){ var json = eval('(' + str + ')'); return json;  ...