ASP.NET Web.Config 读资料 (学习笔记)

时间:2021-09-18 01:31:04

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());