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