如何在web.config中存储自定义对象

 原文: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

 

  
  
  
  
  1. //数据存放在web.config中形如: 
  2.  
  3. <appSettings> 
  4.  
  5.         <add key="WelcomeMessage" value="Hello All, Welcome to my Website." /> 
  6.  
  7. </appSettings> 
  8.  
  9.  
  10.  
  11. // 读取 
  12.  
  13. string message = ConfigurationManager.AppSettings["WelcomeMessage"]; 

 

如今,要是想要经过程序修改appSettings,咱们能够这样作:.net

 

  
  
  
  
  1. //修改 
  2.  
  3.         Configuration config = WebConfigurationManager.OpenWebConfiguration(Request.ApplicationPath); 
  4.  
  5.         config.AppSettings.Settings["WelcomeMessage"].Value = "Hello All, Welcome to my updated site."
  6.  
  7.         config.Save(); 

接着,若是想要在web.config里增长数据怎么作呢,以下:code

 

  
  
  
  
  1. //增长 
  2.  
  3.         Configuration config = WebConfigurationManager.OpenWebConfiguration(Request.ApplicationPath); 
  4.  
  5.         config.AppSettings.Settings.Add("ErrorMessage", "An error has been occured during processing  
  6.  
  7.  
  8.  
  9. this request."); 
  10.  
  11.         config.Save(); 

上面的代码是在web.config里新增新的键值对。如今咱们能够在应用程序的任何地方读取它了。

如今,摆在面前的问题是,咱们是否能够在配置里存储自定义的数据。

固然能够...

咱们能够存储对象。让咱们一块儿来看看怎么作。

我建立了一个样例程序。在这个例子中,我在web.config文件里保存了一个自定义的NewError类的一个对象实例。同时

 

当须要时对其进行修改。

请按照下面的步骤开始实现它:-)

a)建立一个继承自ConfigurationSection(该类在System.Configuration空间能够找到)的类。每一个属性(Porperty)必

 

须有一个属性(attribute)ConfigurationProperty(带有属性名和一些参数,这个名字会直接映射到web.config),让我

 

们看下NewError类:

 

  
  
  
  
  1. public class NewError:ConfigurationSection 
  2.  
  3.  
  4.     //译注:每个属性(Porperty:get,set的那玩意;-))都对应有个属性Attribute【[ConfigurationProperty 】 
  5.  
  6.     [ConfigurationProperty ("Id",IsRequired = true)] 
  7.  
  8.     public string ErrorId { 
  9.  
  10.         get { return (string)this["Id"]; } 
  11.  
  12.         set { this["Id"] = value; } 
  13.  
  14.     } 
  15.  
  16.     [ConfigurationProperty("Message", IsRequired = false)] 
  17.  
  18.     public string Message { 
  19.  
  20.         get { return (string)this["Message"]; } 
  21.  
  22.         set { this["Message"] = value; } 
  23.  
  24.     } 
  25.  
  26.     [ConfigurationProperty("RedirectURL", IsRequired = false)] 
  27.  
  28.     public string RedirectionPage 
  29.  
  30.     { 
  31.  
  32.         get { return (string)this["RedirectURL"]; } 
  33.  
  34.         set { this["RedirectURL"] = value; } 
  35.  
  36.     } 
  37.  
  38.     [ConfigurationProperty("MailId", IsRequired = false)] 
  39.  
  40.     public string EmailId 
  41.  
  42.     { 
  43.  
  44.         get { return (string)this["MailId"]; } 
  45.  
  46.         set { this["MailId"] = value; } 
  47.  
  48.     } 
  49.  
  50.     [ConfigurationProperty("DateAdded", IsRequired = false)] 
  51.  
  52.     public DateTime DateAdded 
  53.  
  54.     { 
  55.  
  56.         get { return (DateTime)this["DateAdded"]; } 
  57.  
  58.         set { this["DateAdded"] = value; } 
  59.  
  60.     } 
  61.  

和你看到的同样,每一个属性有属性Configuration及一些值,好比属性ErrorId:

 

  
  
  
  
  1. [ConfigurationProperty ("Id",IsRequired = true)] 

意思是ErrorId在web.config里存为Id并且不能够缺乏的必要字段。还有一些可选元素根据须要使用。

咱们在深刻看看这个属性,它有些不一样:

 

  
  
  
  
  1. public string ErrorId { 
  2.  
  3. get { return (string)this["Id"]; } 
  4.  
  5. set { this["Id"] = value; } 
  6.  

在这里value值存为键"id",和web.config里对应。

b.)如今须要要作的就是在section组注册、添加一个section告诉web.config你须要这种类型数据。

必须放在<configSections />里以下:

 

  
  
  
  
  1. <section name="errorList"  type="NewError" allowLocation="true" 
  2.  
  3.      allowDefinition="Everywhere"/> 

c.)如今能够直接在配置文件里添加这样的对象了:

 

  
  
  
  
  1. <errorList Id="1" Message="ErrorMessage" RedirectURL="www.google.com" MailId="xyz@hotmail.com"  
  2.  
  3.  
  4.  
  5. ></errorList> 

d.)而后在你的页面这样读取:

NewError objNewError = (NewError)ConfigurationManager.GetSection("errorList");

也能够经过程序方式保存一个新对象:

 

  
  
  
  
  1. NewError objNewError = new NewError() 
  2.  
  3.         { 
  4.  
  5.           RedirectionPage="www.rediff.com"
  6.  
  7.           Message = "New Message"
  8.  
  9.           ErrorId="0"
  10.  
  11.           DateAdded= DateTime.Now.Date 
  12.  
  13.         }; 
  14.  
  15.         Configuration config = 
  16.  
  17.             WebConfigurationManager.OpenWebConfiguration(Request.ApplicationPath); 
  18.  
  19.  
  20.  
  21.         config.Sections.Add("errorList", objNewError); 
  22.  
  23.         config.Save(); 

甚至能够添加一个自定义的组,而后在此section区放置一些自定义元素。

Asp.net提供给咱们很是强大的编程接口API,咱们能够很轻松的读取/编辑web.config文件。

最后但愿您心情愉悦的浏览此文,也很是感激您的回馈信息。

Thanks.

相关文章
相关标签/搜索