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=" ...
随机推荐
- [LeetCode] 30. Substring with Concatenation of All Words 解题思路 - Java
You are given a string, s, and a list of words, words, that are all of the same length. Find all sta ...
- 首届京东自有品牌科技周“京东点亮生活”圆满成功 - 课程公告板 - 京东内部论坛 - Powered by Discuz!
首届京东自有品牌科技周"京东点亮生活"圆满成功 - 课程公告板 - 京东内部论坛 - Powered by Discuz! 首届京东自有品牌科技周"京东点亮生活" ...
- E - Minimum Cost - POJ 2516(最小费)
题目大意:N个客户,M个供货商,K种商品,现在知道每个客户对每种商品的需求量,也知道每个供货商每种商品的持有量,和供货商把一种商品运送到每个客户的单位花费.现在想知道如果能满足所有客户的最小花费是多少 ...
- javascript基础笔记学习
/** * Created by Administrator on 2016/12/26. */ /* var box; alert( typeof box); box是Undefined类型,值是u ...
- Json对象直接存取数据库
需要jar包: json.jar jackson-core.jar jacson-mapper.jar 一.ArrayList转json和hashmap转json代码一样 /** * 把map转换成 ...
- XML FREESWITCH APPLICATION 实现
XML XML在FS(FreeSwitch)中进行了大量的使用,其中dialplan就是其中非常主要的一快内容.通过下面的描述,我们可以知道在执行XML中的语句: <action applica ...
- swift开发笔记24 解决键盘遮挡输入框 的方法
很简单,就是开始输入时把整个view的frame上移,也就是把y值减小就行了,至于减少多少自己调 ,也可以动态获取参见(http://blog.csdn.net/lengshengren/articl ...
- Android(java)学习笔记252:ContentProvider使用之内容观察者01
1. 内容观察者 不属于四大组件,只是内容提供者ContentProvider对应的小功能. 如果发现数据库内容变化了,就会立刻观察到. 下面是逻辑图: 当A应用中银行内部的数据发生变化的 ...
- Linux学习计划
用Linux已经有很长一段时间了,先后用过Debian和CentOS,但一直没有系统地去学习,现在一是有时间,二是工作中需要用到,所以准备用心去学习一下. 一.教材 1.主要以<鸟哥Linux& ...
- 【Asp.Net】后台生成控件并绑定事件
在Asp.Net的Web页面处理流程中,有时候我们会碰到需要动态生成的控件,并为之绑定相应的事件. 接下来我们来动态的生成一个控件 //在用户代码初始化阶段添加控件 protected void Pa ...