ASP.NET Web.Config 读资料 (学习笔记)
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="50" /> </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="50" /> </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="50" /> <objectCs> <objectC Id="1" stringData="x" /> <objectC Id="2" stringData="y" /> <objectC Id="3" 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; //50 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="11111"></add> <add key="bb" value="22222"></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());
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 一个费力不讨好的项目,让我损失了近一半的绩效!
· .NET Core 托管堆内存泄露/CPU异常的常见思路
· PostgreSQL 和 SQL Server 在统计信息维护中的关键差异
· C++代码改造为UTF-8编码问题的总结
· DeepSeek 解答了困扰我五年的技术问题
· 清华大学推出第四讲使用 DeepSeek + DeepResearch 让科研像聊天一样简单!
· 推荐几款开源且免费的 .NET MAUI 组件库
· 实操Deepseek接入个人知识库
· 易语言 —— 开山篇
· 【全网最全教程】使用最强DeepSeekR1+联网的火山引擎,没有生成长度限制,DeepSeek本体