原文:http://www.codeproject.com/Articles/184716/How-to-store-custom-objects-in-web-config.aspxweb
如何在web.config中存储自定义对象编程
在本文中将要讨论web.config.在我平日里,都习惯把一些数据放在web.config的appSettings节里,当须要时再读取。app
那都是string字符串的形式。固然了不止这些,咱们也能够经过编程方式修改这些数据。ide
如今重要的一点须要说的是,咱们也能够在web.config里存放自定义的类型,虽然一般状况不这么作。可是在不少情形ui
下,这样作颇有用。this
你们是否尝试过修改或者添加过web.config里的一些数值。咱们先简单的讨论下这个:google
首先,这些都是日常常见到的,把一些常量数据放在web.config的appSettings节,根据须要读取。那么怎么读呢(对初学者):spa
- //数据存放在web.config中形如:
- <appSettings>
- <add key="WelcomeMessage" value="Hello All, Welcome to my Website." />
- </appSettings>
- // 读取
- string message = ConfigurationManager.AppSettings["WelcomeMessage"];
如今,要是想要经过程序修改appSettings,咱们能够这样作:.net
- //修改
- Configuration config = WebConfigurationManager.OpenWebConfiguration(Request.ApplicationPath);
- config.AppSettings.Settings["WelcomeMessage"].Value = "Hello All, Welcome to my updated site.";
- config.Save();
接着,若是想要在web.config里增长数据怎么作呢,以下:code
- //增长
- Configuration config = WebConfigurationManager.OpenWebConfiguration(Request.ApplicationPath);
- config.AppSettings.Settings.Add("ErrorMessage", "An error has been occured during processing
- this request.");
- config.Save();
上面的代码是在web.config里新增新的键值对。如今咱们能够在应用程序的任何地方读取它了。
如今,摆在面前的问题是,咱们是否能够在配置里存储自定义的数据。
固然能够...
咱们能够存储对象。让咱们一块儿来看看怎么作。
我建立了一个样例程序。在这个例子中,我在web.config文件里保存了一个自定义的NewError类的一个对象实例。同时
当须要时对其进行修改。
请按照下面的步骤开始实现它:-)
a)建立一个继承自ConfigurationSection(该类在System.Configuration空间能够找到)的类。每一个属性(Porperty)必
须有一个属性(attribute)ConfigurationProperty(带有属性名和一些参数,这个名字会直接映射到web.config),让我
们看下NewError类:
- public class NewError:ConfigurationSection
- {
- //译注:每个属性(Porperty:get,set的那玩意;-))都对应有个属性Attribute【[ConfigurationProperty 】
- [ConfigurationProperty ("Id",IsRequired = true)]
- public string ErrorId {
- get { return (string)this["Id"]; }
- set { this["Id"] = value; }
- }
- [ConfigurationProperty("Message", IsRequired = false)]
- public string Message {
- get { return (string)this["Message"]; }
- set { this["Message"] = value; }
- }
- [ConfigurationProperty("RedirectURL", IsRequired = false)]
- public string RedirectionPage
- {
- get { return (string)this["RedirectURL"]; }
- set { this["RedirectURL"] = value; }
- }
- [ConfigurationProperty("MailId", IsRequired = false)]
- public string EmailId
- {
- get { return (string)this["MailId"]; }
- set { this["MailId"] = value; }
- }
- [ConfigurationProperty("DateAdded", IsRequired = false)]
- public DateTime DateAdded
- {
- get { return (DateTime)this["DateAdded"]; }
- set { this["DateAdded"] = value; }
- }
- }
和你看到的同样,每一个属性有属性Configuration及一些值,好比属性ErrorId:
- [ConfigurationProperty ("Id",IsRequired = true)]
意思是ErrorId在web.config里存为Id并且不能够缺乏的必要字段。还有一些可选元素根据须要使用。
咱们在深刻看看这个属性,它有些不一样:
- public string ErrorId {
- get { return (string)this["Id"]; }
- set { this["Id"] = value; }
- }
在这里value值存为键"id",和web.config里对应。
b.)如今须要要作的就是在section组注册、添加一个section告诉web.config你须要这种类型数据。
必须放在<configSections />里以下:
- <section name="errorList" type="NewError" allowLocation="true"
- allowDefinition="Everywhere"/>
c.)如今能够直接在配置文件里添加这样的对象了:
- <errorList Id="1" Message="ErrorMessage" RedirectURL="www.google.com" MailId="xyz@hotmail.com"
- ></errorList>
d.)而后在你的页面这样读取:
NewError objNewError = (NewError)ConfigurationManager.GetSection("errorList");
也能够经过程序方式保存一个新对象:
- NewError objNewError = new NewError()
- {
- RedirectionPage="www.rediff.com",
- Message = "New Message",
- ErrorId="0",
- DateAdded= DateTime.Now.Date
- };
- Configuration config =
- WebConfigurationManager.OpenWebConfiguration(Request.ApplicationPath);
- config.Sections.Add("errorList", objNewError);
- config.Save();
甚至能够添加一个自定义的组,而后在此section区放置一些自定义元素。
Asp.net提供给咱们很是强大的编程接口API,咱们能够很轻松的读取/编辑web.config文件。
最后但愿您心情愉悦的浏览此文,也很是感激您的回馈信息。
Thanks.