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=""
/>
</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 读资料 (学习笔记)的更多相关文章
- ASP.NET Web.config学习
花了点时间整理了一下ASP.NET Web.config配置文件的基本使用方法.很适合新手参看,由于Web.config在使用很灵活,可以自定义一些节点.所以这里只介绍一些比较常用的节点. <? ...
- ASP.NET web.config中数据库连接字符串connectionStrings节的配置方法
ASP.NET web.config中数据库连接字符串connectionStrings节的配置方法 第一种情况,本地开发时,使用本地数据库,如下面的代码 <connectionStrings& ...
- Asp.net Web.Config - 配置元素 caching
Asp.net Web.Config - 配置元素 caching 记得之前在写缓存DEMO的时候,好像配置过这个元素,好像这个元素还有点常用. 一.caching元素列表 元素 说明 cache ...
- 【笔记目录2】【jessetalk 】ASP.NET Core快速入门_学习笔记汇总
当前标签: ASP.NET Core快速入门 共2页: 上一页 1 2 任务27:Middleware管道介绍 GASA 2019-02-12 20:07 阅读:15 评论:0 任务26:dotne ...
- asp.net web.config的学习笔记
原文地址:http://www.cnblogs.com/Bulid-For-NET/archive/2013/01/11/2856632.html 一直都对web.config不太清楚.这几天趁着项目 ...
- ASP.NET Web.config文件的配置(Configuration API)
本次我们讨论主要聚焦在以下Web.config配置文件的设置值的读取. 1.<connectionString />连接字符串的读取. 2.<appSettings />应用程 ...
- ASP.NET Web.config
分析: .NET Web 应用程序的配置信息(如最常用的设置ASP.Net Web 应用程序的身份验证方式),它可以出现在应用程序的每一个目录中.当你通过VB.NET新 建 一个Web应用程序后,默认 ...
- 【Pro ASP.NET MVC 3 Framework】.学习笔记.5.SportsStore一个真实的程序
我们要建造的程序不是一个浅显的例子.我们要创建一个坚固的,现实的程序,坚持使它成为最佳实践.与Web Form中拖控件不同.一开始投入MVC程序付出利息,它给我们可维护的,可扩展的,有单元测试卓越支持 ...
- ASP.NET web.config中的连接字符串
在ASP.NET的web.config中,可以用两种方式来写连接字符串的配置. <configuration> <appSettings> <add key=" ...
随机推荐
- 《Linear Algebra and Its Applications》-chaper1-线性方程组-线性相关性
这篇文章主要简单的记录所谓的“线性相关性”. 线性相关性的对象是向量R^n,对于向量方程,如果说x1v1 + x2v2 + …+xmvm = 0(其中xi是常数,vi是向量)有且仅有一个平凡解,那么我 ...
- 【转】两种方法教你在Ubuntu下轻松关闭触摸板(TinkPad)
Ubuntu是一个以桌面应用为主的Linux操作系统,所以在使用时我经常的触碰到触摸板,这样会造成我们一些的麻烦,所以要如何的关闭触摸板呢?我们一起来看看吧! Ubuntu下如何关闭触摸板(Tin ...
- implode 多维数组转一维数组并字符串输出
//多维数组返回一维数组,拼接字符串输出 public function r_implode( $glue, $pieces ) { foreach( $pieces as $r_pieces ) { ...
- 利用PyQt4写的小工具软件
应公司文职工作人员需求,写一个车间人员工作时间的统计软件,输入开始工作时间1,再输入结束工作时间2,计算两个时间的差值. 根据需求,初步构想的UI界面如下: 下面开始干活. 分析后觉得利用PyQt4来 ...
- android:ellipsize的使用
EidtText和textview中内容过长的话自动换行,使用android:ellipsize与android:singleine可以解决,使只有一行. EditText不支持marquee 用法如 ...
- 《JAVA课程设计》实训第四天——《猜猜看》游戏
第四天,本来想进一步去改进<猜猜看>游戏的.可是非常多问题都不理解.也不熟悉怎么去弄到连接数据库.统计猜对次数,所以并没有进行再多的改动. 基本上就是这种执行结果了 import java ...
- cocos2d 高仿doodle jump 无源代码
1. 游戏视频 主角眼熟吗?没错,上次跑酷游戏中的"30"来Jump了,有三种道具.主角光环,竹蜻蜓.翅膀: 有两种怪物,螃蟹和鸟: 有5种板子.点击屏幕,30会把它的嘴巴3给发射 ...
- 单向链表JAVA代码
//单向链表类 publicclassLinkList{ //结点类 publicclassNode{ publicObject data; ...
- Javascipt 时间格式化(日期)
Date.prototype.format =function(format){ var o = { "M+" : this.getMonth()+1, //month " ...
- oracle存储过程调试方法
PL/SQL中为我们提供了[调试存储过程]的功能,可以帮助你完成存储过程的预编译与测试. 点击要调试的存储过程,右键选择TEST 如果需要查看变量,当然调试都需要.在右键菜单中选择Add debug ...