如何在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.

相關文章
相關標籤/搜索