master2012

导航

.net webconfig configsections

    asp.net为啥要引用configSections?
因为用户的一些对象, 可能在config里进行配置, 但是config怎么能随便让你添加自己的节点呢! 不行你自己试试, 在任何位置添加任何没有申明的节点, 系统都不会让你通过, 更不会让你去读它了, 当然, 你打算在别的xml文件里添加节点, 然后读出来, 创建对象, 这个没问题.
 
为了系统能有组织的管理用户的在配置文件里的自定义信息, 就要使用configSections了, 分3步走:
  1. 创建一个实现IConfigurationSectionHandler的类
 
  2. 在config文件里添加如下节点:
 
  <configuration>
              <configSections>
                   <section name="NameM" type="LearningConfiguration.NameSectionHandler"/>
              </configSections>
              <NameM>
                   <Add key="name1" firstname="Jim" lastname="w1"/>
                   <Add key="name2" firstname="Chris" lastname="w2"/>
               </NameM>
    
        说明: 在configSections下添加一个section子节点, 写好标示名和类型, 然后在紧接这下面实现这个标示名的细节内容,就是我们当初想添加的自己的自定义信息了.
       
    3. 实现IConfigurationSectionHandler唯一的方法: public object Create(object parent, object configContext, XmlNode section), 这里的最后一个参数就是自定义信息那个节点了, 粗体部分. 通过处理这堆信息, 返回你要的对象.
 
       4. 使用这个返回的对象,通过System.Configuration.ConfigurationManager.GetSection()方法.
 
以下是示例代码:
(NameSectionHandler.cs)

namespace LearningConfiguration
{
    
public class NameSectionHandler : IConfigurationSectionHandler
    {
        
#region IConfigurationSectionHandler Members
        
public object Create(object parent, object configContext, XmlNode section)
        {
            Dictionary
<string, NameManagement> names = new Dictionary<string, NameManagement>();
            
            
string key = string.Empty;
            
string firstName = string.Empty;
            
string lastName = string.Empty;
            
foreach (XmlNode childNode in section.ChildNodes)
            {
                
if (childNode.Attributes["key"!= null)
                {
                    key 
= childNode.Attributes["key"].Value;

                    
if (childNode.Attributes["firstname"!= null)
                    {
                        firstName 
= childNode.Attributes["firstname"].Value;
                    }
                    
else
                    {
                        firstName 
= string.Empty;
                    }
                    
if (childNode.Attributes["lastname"!= null)
                    {
                        lastName 
= childNode.Attributes["lastname"].Value;
                    }
                    
else
                    {
                        lastName 
= string.Empty;
                    }
                    names.Add(key, 
new NameManagement(firstName, lastName));
                }
            }
            
return names;
        }
        
#endregion
    }
}
(NameManagement.cs)
复制代码
namespace LearningConfiguration
{
    
public class NameManagement
    {
        
string _firstName;
        
public string FirstName
        {
            
get { return this._firstName; }
            
set { this._firstName = value; }
        }
        
string _lastName;
        
public string LastName
        {
            
get { return this._lastName; }
            
set { this._lastName = value; }
        }
        
public NameManagement(string firstName, string lastName)
        {
            
this.FirstName = firstName;
            
this.LastName = lastName;
        }
        
public string RetrieveFullName()
        {
            
return string.Format("{0} {1}"this.FirstName, this.LastName);
        }
    }
}
复制代码
(Default.aspx.cs)
复制代码
namespace LearningConfiguration
{
    
public partial class _Default : System.Web.UI.Page
    {
        
protected void Page_Load(object sender, EventArgs e)
        {
            Dictionary
<string, NameManagement> names = ConfigurationManager.GetSection("NameM"as Dictionary<string, NameManagement>;
            
if (names != null)
            { 
                
foreach(string key in names.Keys)
                {
                    NameManagement name 
= names[key] as NameManagement;
                    Response.Write(name.RetrieveFullName());
                }
            }
        }
    }
}
复制代码
 与以上调用方式一样:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Collections.Specialized;
using LearningConfiguation;

public partial class Default4 : System.Web.UI.Page
{
    
protected void Page_Load(object sender, EventArgs e)
    {
        Dictionary
<String, NameManagement> names = System.Configuration.ConfigurationManager.GetSection("test/aa"as Dictionary<String, NameManagement>;
        Dictionary
<String, NameManagement>.KeyCollection keys = names.Keys;
        
foreach (String key in keys)
        {
            NameManagement nm 
= names[key] as NameManagement;
            Response.Write(nm.FirstName);
            Response.Write(
"<br>");
            Response.Write(nm.LastName);
        }
    }
}

posted on 2013-06-06 15:04  master2012  阅读(163)  评论(0)    收藏  举报