基于可配置化的设计[原创][4.20更新]

[有关于EL的Configuration Block和自定化配置]
这里一共写了三种配置方法,第一种是EL的写法,第二种是ELQuickStart的写法,第三种是一般的写法
第一种配置写法
None.gif    调用处:
ExpandedBlockStart.gifContractedBlock.gif        
/**/ ///Found Domain and Title in the CooperationChannelsData.xml
None.gif         ChannelConfig channelConfigs  =  MasterPageHelper.ListChannelProperty();
None.gif
None.gif
-------------------------------------------------------------------
None.gif
None.gif调用方法:
None.gif
ContractedBlock.gifExpandedBlockStart.gif        
SearchDomain #region SearchDomain
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 查找配置文件中的Domain
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        public static ChannelProperty SearchDomain()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
string serverName = HttpContext.Current.Request.ServerVariables["server_name"].ToLower();
InBlock.gif
InBlock.gif            ChannelProperty defaultChannel 
= Channels.DefaultChannel.Get(0);
InBlock.gif
InBlock.gif           
//这里我们做一个DefaultChannel
InBlock.gif
            if (defaultChannel.Name.Equals(serverName))
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return defaultChannel;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
else
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{  //做一个其他 Channel
InBlock.gif
                foreach (ChannelProperty data in Channels.Channels)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    String[] strings 
= SplitString(data.Name);
InBlock.gif
InBlock.gif                    
foreach (String name in strings)
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        
if (name.Trim().Equals(serverName))
ExpandedSubBlockStart.gifContractedSubBlock.gif                        
dot.gif{
InBlock.gif                            
return data;
ExpandedSubBlockEnd.gif                        }

ExpandedSubBlockEnd.gif                    }

ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif            
return defaultChannel;
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 返回处理
InBlock.gif        
/// </summary>
ExpandedSubBlockEnd.gif        
/// <returns></returns>

InBlock.gif        public static ChannelConfigs ListChannelProperty()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            ChannelConfigs configs 
= new ChannelConfigs(SearchDomain());   
InBlock.gif            
InBlock.gif            
//为什么不直接返回ChannelPropertys,因为ChannelPropertys中的属性是只能Get的,
InBlock.gif            
//所以也是由于处理考虑所以使用了把ChannelPropertys直接Copy给ChannelConfigs,
InBlock.gif            
//然后对ChannelConfigs进行Set的处理,最后返回一个ChannelConfigs.
InBlock.gif            
//这样性能是有开销的。但是也没有想什么好的方法?
InBlock.gif

InBlock.gif            GetCookieProperty(configs);
InBlock.gif
InBlock.gif            
return configs;
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedBlockEnd.gif        
#endregion

None.gif
ContractedBlock.gifExpandedBlockStart.gif        
GetCookieProperty #region GetCookieProperty
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        public static void GetCookieProperty(ChannelConfigs configs)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            String serverName 
= GetServerName();
InBlock.gif
InBlock.gif            
//域名为XXX
InBlock.gif
            if (XXX.Equals(configs.DomainName))
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
if (HttpContext.Current.Request.Cookies[XXX] != null)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
//Url
InBlock.gif
                    configs.HeaderUrl = "www.HeaderUrl.com";
InBlock.gif                    configs.FooterUrl 
= "www.footerurl.com";
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedBlockEnd.gif        
#endregion

None.gif
ContractedBlock.gifExpandedBlockStart.gif        
SplitString #region SplitString
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// Split String
InBlock.gif        
/// Split Symbol is ":"
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        public static String[] SplitString(String src)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
return src.Split(SPLITSYMBOL);
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedBlockEnd.gif        
#endregion

None.gif
None.gif
None.gif
-------------------------------------------------------------------
None.gif
None.gif
// ************************************************************************************
None.gif
//  功能:ChannelCollection
None.gif
//  
None.gif
// ************************************************************************************ 
None.gif
namespace  Ctrip.UI.DomesticFlight
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
using System;
InBlock.gif    
using System.Collections.Generic;
InBlock.gif    
using System.Text;
InBlock.gif    
using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;
InBlock.gif    
using System.Configuration;
InBlock.gif
InBlock.gif    
public class CooperationSettings : SerializableConfigurationSection
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
public const string SectionName = "CooperationChannels";
InBlock.gif        
private const string channelSettingsProperty = "ChannelSettings";
InBlock.gif        
private const string DefaultChannelProperty = "DefaultChannel";       
InBlock.gif
InBlock.gif        [ConfigurationProperty(channelSettingsProperty, IsRequired 
= true)]
InBlock.gif        
public NamedElementCollection<ChannelProperty> Channels
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get dot.gifreturn (NamedElementCollection<ChannelProperty>)base[channelSettingsProperty]; }
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        [ConfigurationProperty(DefaultChannelProperty, IsRequired 
= true)]
InBlock.gif        
public NamedElementCollection<ChannelProperty> DefaultChannel
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get dot.gifreturn (NamedElementCollection<ChannelProperty>)base[DefaultChannelProperty]; }
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif
None.gif
-------------------------------------------------------------------
None.gif
None.gif
// ************************************************************************************
None.gif
//  功能:ConfigurationData
None.gif
//  
None.gif
// ************************************************************************************ 
None.gif
namespace  XXXX
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
using System;
InBlock.gif    
using System.Text;
InBlock.gif    
using System.Configuration;
InBlock.gif    
using System.Collections.Generic;
InBlock.gif    
using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;
InBlock.gif    
using Microsoft.Practices.EnterpriseLibrary.Common.Configuration.ObjectBuilder;
InBlock.gif
InBlock.gif    
public class ChannelProperty : NamedConfigurationElement
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
ContractedSubBlock.gifExpandedSubBlockStart.gif        
Define Params#region Define Params
InBlock.gif
InBlock.gif        
private const string builderNameProperty = "builderName";
InBlock.gif
ExpandedSubBlockEnd.gif        
#endregion

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        
Property#region Property
InBlock.gif
InBlock.gif        [ConfigurationProperty(nameProperty, IsKey 
= true, DefaultValue = "Name", IsRequired = true)]
InBlock.gif        [StringValidator(MinLength 
= 1)]
InBlock.gif        
public new string Name
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get dot.gifreturn (string)this[nameProperty]; }
ExpandedSubBlockStart.gifContractedSubBlock.gif            
set dot.gifthis[nameProperty] = value; }
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
//Name的这个属性我重写了,把Name属性变为自己所用。变成唯一的Key
InBlock.gif

InBlock.gif        [ConfigurationProperty(builderNameProperty, IsRequired 
= true)]
InBlock.gif        
public string BuilderName
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get dot.gifreturn (string)base[builderNameProperty]; }
ExpandedSubBlockStart.gifContractedSubBlock.gif            
set dot.gifbase[builderNameProperty] = value; }
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
//IsRequired = true :意思为Config文件中必须中BuilderName
InBlock.gif        
//IsRequired = false :意思为Config文件中不必须有BuilderName
InBlock.gif

ExpandedSubBlockEnd.gif        
#endregion

ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    
public class ChannelConfigs
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
ContractedSubBlock.gifExpandedSubBlockStart.gif        
Init#region Init
InBlock.gif
InBlock.gif        
public ChannelConfigs(ChannelProperty propertys)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            name 
= propertys.Name;
ExpandedSubBlockEnd.gif        }
 
InBlock.gif
ExpandedSubBlockEnd.gif        
#endregion

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        
Property#region Property
InBlock.gif
InBlock.gif        
private string name;
InBlock.gif        
public string Name
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get dot.gifreturn name; }
ExpandedSubBlockStart.gifContractedSubBlock.gif            
set dot.gif{ name = value; }
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockEnd.gif        
#endregion

InBlock.gif        
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif
None.gif
-------------------------------------------------------------------
None.gif
None.gif配置文件:
None.gif
None.gif
<? xml version = " 1.0 "  encoding = " gb2312 " ?>
None.gif
< CooperationChannels >
None.gif  
< ChannelSettings >
None.gif    
<!-- Ctrip -->
None.gif    
<!-- using  Test -->
None.gif    
< add builderName = " ctripDomain "  
None.gif          domainName
= " ctrip "  
None.gif          name
= " flights.dev.sh.com:
None.gif
                localhost "
None.gif
          baseTitle = " XXXXXXXXXxx "
None.gif          cssSrc
= " /css/Style.css:
None.gif
                   / css / FlightSearch.css:
None.gif                  
/ styles / common / public_global.css:
None.gif                  
/ styles / fltDomestic / public_fltDomestic.css:
None.gif                  
/ css / private_fltDomestic_choice.css:
None.gif                  
/ css / private_fltDomestic_login.css:
None.gif                  
/ css / private_fltDomestic_query_again.css:
None.gif                  
/ css / public_memCenter.css:
None.gif                  
/ css / private_memCenter_sidebar.css:
None.gif                  
/ css / private_memCenter_orderManage.css:
None.gif                  
/ css / private_fltDomestic_order01.css:
None.gif                  
/ css / public_fltDomestic01.css:
None.gif                  
/ css / public_global01.css "  
None.gif
          jsSrc = " /JSCRIPT/__utm.js:/JSCRIPT/MasterPage.js:/JSCRIPT/tail_base.js "  
None.gif          metaDecContent
= " XXXXXXXXXXXXXXXXx "  
None.gif          metaKeyWordContent
= " XXXXXXXXXXXXXXxx "  
None.gif          headHeight
= " 205px "  
None.gif          headWidth
= " 100% "
None.gif          footHeight
= " 200px "  
None.gif          footWidth
= " 100% " />
None.gif  
</ ChannelSettings >
None.gif  
None.gif  
<!-- Ctrip -->
None.gif  
< DefaultChannel >
None.gif    
< add builderName = " ctripDomain "  
None.gif           domainName
= " XXXXX "  
None.gif           name
= " www.baidu.com "
None.gif           baseTitle
= " XXXXXXXXXXXxxx "
None.gif           cssSrc
= " /css/Style.css:
None.gif
                    / css / FlightSearch.css:
None.gif                   
/ styles / common / public_global.css:
None.gif                   
/ styles / fltDomestic / public_fltDomestic.css:
None.gif                   
/ css / private_fltDomestic_choice.css:
None.gif                   
/ css / private_fltDomestic_login.css:
None.gif                   
/ css / private_fltDomestic_query_again.css:
None.gif                   
/ css / public_memCenter.css:
None.gif                   
/ css / private_memCenter_sidebar.css:
None.gif                   
/ css / private_memCenter_orderManage.css:
None.gif                   
/ css / private_fltDomestic_order01.css:
None.gif                   
/ css / public_fltDomestic01.css:
None.gif                   
/ css / public_global01.css "  
None.gif
           jsSrc = " /JSCRIPT/__utm.js:/JSCRIPT/MasterPage.js:/JSCRIPT/tail_base.js "  
None.gif           metaDecContent
= " XXXXXXXXXXXXXXXXXXXXXXx "  
None.gif           metaKeyWordContent
= " XXXXXXXXXXXXXXXx "  
None.gif           headHeight
= " 205px "  
None.gif           headWidth
= " 100% "
None.gif           footHeight
= " 200px "  
None.gif           footWidth
= " 100% " />
None.gif  
</ DefaultChannel >
None.gif  
None.gif
</ CooperationChannels >
None.gif
None.gif
-------------------------------------------------------------------

第二种配置方法的写法
None.gif 1 .有关于自定义配置
None.gif
----------------------------------------------------------------------------------------------------------------------------------------------------
None.gifMasterPage:      
None.gif    
private   void  GetRelatedDomainAndSetPageTitle()
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif {
InBlock.gif            NameValueCollection channelCollection 
= new CooperationChannelsConfig().Settings;
InBlock.gif
InBlock.gif            
foreach (String channel in channelCollection)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
if (channel.Equals(UIHelper.GetServerName(this.Page)))
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    DomainName 
= channelCollection[channel].Split('|')[0].ToString();
InBlock.gif                    
this.Page.Title = channelCollection[channel].Split('|')[1].ToString();
InBlock.gif                    
break;
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif            
//默认域名[hp]下
InBlock.gif
            if (String.IsNullOrEmpty(DomainName) == true)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                DomainName 
= "hp";
InBlock.gif                
this.Page.Title = "默认标题
InBlock.gif
-";          }
ExpandedSubBlockEnd.gif
        }

InBlock.gif
InBlock.gif
----------------------------------------------------------------------------------------------------------------------------------------------------
InBlock.gif
InBlock.gifCooperationChannelsConfig :
InBlock.gif
InBlock.gif
namespace NameSpace
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif    
public class CooperationChannelsConfig : IConfigurationSectionHandler
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// 实现一个类支持IConfigurationSectionHandler 接口来对自定义节进行处理,完成对自定义节的读取
InBlock.gif        public object Create(object parent, object configContext, XmlNode section)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            NameValueCollection settings;
InBlock.gif
InBlock.gif            
try
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                NameValueSectionHandler baseHandler 
= new NameValueSectionHandler();
InBlock.gif                settings 
= (NameValueCollection)baseHandler.Create(parent, configContext, section);
ExpandedSubBlockEnd.gif            }

InBlock.gif            
catch
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                settings 
= null;
ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif            
return settings;
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 返回整个Channel
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        public NameValueCollection Settings
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return (NameValueCollection)ConfigurationManager.GetSection("channel");
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

ExpandedSubBlockEnd.gif}

InBlock.gif
InBlock.gif
----------------------------------------------------------------------------------------------------------------------------------------------------
InBlock.gif
InBlock.gifGlobal:
InBlock.gifSystem.Configuration.ConfigurationManager.GetSection(
"channel"); 
InBlock.gif
InBlock.gif
----------------------------------------------------------------------------------------------------------------------------------------------------
InBlock.gif
InBlock.gifWeb.config
InBlock.gif    
<section name="channel" type="NameSpace.CooperationChannelsConfig, NameSpace" />
InBlock.gif  
<channel configSource="Config\\Channel.config"/>
InBlock.gif
InBlock.gif
----------------------------------------------------------------------------------------------------------------------------------------------------
InBlock.gif
InBlock.gifChannel.config:
InBlock.gif
InBlock.gif
<?xml version="1.0" encoding="gb2312"?>
InBlock.gif
<channel>
InBlock.gif  
<add key="gotone.smcc.flights.ctrip.com" value="mc_group|"/>
InBlock.gif  
<!--特商外包-->
InBlock.gif  
<add key="xinhuanet.flights.ctrip.com" value="xinhuanet|新华网"/>
InBlock.gif  
<add key="flights.ctrip.xinhuanet.com" value="ctripxinhuanet|央视国际"/>
InBlock.gif  
<add key="cctv.flights.ctrip.com" value="cctv|"/>
InBlock.gif  
<add key="airchina.flights.ctrip.com" value="airchina|"/>
InBlock.gif 
</channel>
InBlock.gif
InBlock.gif
----------------------------------------------------------------------------------------------------------------------------------------------------
InBlock.gif

第三种配置的写法
None.gif 配置项目节点属性类:
None.gif
namespace  NameSpace
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
using System.Text;
InBlock.gif    
using System.Configuration;
InBlock.gif    
using String = System.String;
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
InBlock.gif    
/// ChannelProperty 
ExpandedSubBlockEnd.gif    
/// </summary>

InBlock.gif    public class ChannelProperty : ConfigurationSection
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        [ConfigurationProperty(
"urlName")]
InBlock.gif        
public String UrlName
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get dot.gifreturn this["urlName"].ToString(); }
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// DomainName:crp
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        [ConfigurationProperty("domainName")]
InBlock.gif        
public String DomainName
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get dot.gifreturn this["domainName"].ToString(); }
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// baseTitle
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        [ConfigurationProperty("baseTitle")]
InBlock.gif        
public String BaseTitle
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get dot.gifreturn this["baseTitle"].ToString(); }
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        [ConfigurationProperty("cssSrc")]
InBlock.gif        
public String CssSrc
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get dot.gifreturn this["cssSrc"].ToString(); }
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif
----------------------------------------------------------------------------------------------
None.gif
None.gif
namespace  NameSpace
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
using System;
InBlock.gif    
using System.Collections.Generic;
InBlock.gif    
using System.Text;
InBlock.gif    
using System.Configuration;
InBlock.gif
InBlock.gif    [ConfigurationCollection(
typeof(ChannelProperty))]
InBlock.gif    
public sealed class ChannelCollection : ConfigurationElementCollection
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
protected override ConfigurationElement CreateNewElement()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
return new ChannelProperty();
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
protected override object GetElementKey(ConfigurationElement element)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
return ((ChannelProperty)element).UrlName;
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    
public sealed class CooperationChannels : ConfigurationSection
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        [ConfigurationProperty(
"ChannelSettings", IsDefaultCollection = false)]
InBlock.gif        [ConfigurationCollection(
typeof(ChannelCollection), AddItemName = "Channel")]
InBlock.gif        
public ChannelCollection Channels
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return base["ChannelSettings"as ChannelCollection;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif
None.gif
----------------------------------------------------------------------------------------------
None.gif
None.gifChannel.Config
None.gif
<? xml version = " 1.0 "  encoding = " gb2312 " ?>
None.gif
< CooperationChannels >
None.gif  
< ChannelSettings >
None.gif    
< Channel domainName = " baidu "  urlName = " www.baidu.com "  baseTitle = " hp- "  cssSrc = ""   />
None.gif    
< Channel domainName = " hp "  urlName = " www.hp.com "  baseTitle = " hp- "  cssSrc = ""   />
None.gif      
</ ChannelSettings >
None.gif
</ CooperationChannels >
None.gif
None.gif
----------------------------------------------------------------------------------------------
None.gif
None.gifGlobal文件:
None.gif 应添加,去读取Web.Config中的节点
None.gif
protected   void  Application_Start( object  sender, EventArgs e)
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif {          System.Configuration.ConfigurationManager.GetSection("CooperationChannels"); 
ExpandedBlockEnd.gif}

None.gif
None.gif
----------------------------------------------------------------------------------------------
None.gif
None.gifWeb.Config
None.gif
None.gif
< configSections >
None.gif    
< section name = " CooperationChannels "  type = " Ctrip.UI.DomesticFlight.CooperationChannels, Ctrip.UI.DomesticFlight "   />
None.gif  
</ configSections >
None.gif
None.gif  
< CooperationChannels configSource = " Config\\Channels.config " />
None.gif
None.gif
----------------------------------------------------------------------------------------------
None.gif
None.gifMasterPage页面调用处:
None.gif
public  partial  class  MasterPage_DA : System.Web.UI.MasterPage
ExpandedBlockStart.gifContractedBlock.gif    
dot.gif {
InBlock.gif ChannelProperty configData 
= null;
InBlock.gif
InBlock.gif            CooperationChannels channelSections 
= ConfigurationManager.GetSection("CooperationChannels"as CooperationChannels;
InBlock.gif            
foreach (ChannelProperty data in channelSections.Channels)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
if (data.UrlName.Equals("www.baidu.com"))
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    configData 
= data;
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

ExpandedBlockEnd.gif}

None.gif
None.gif有关data这个字段,你quickWatch一下。就可以发现Channel.config中的有关于www.baidu.com的节点段中的所有字段都可以取得了。
None.gif

None.gif 最后还是补充一下:
None.gif
1 . protected   abstract  ConfigurationElement CreateNewElement() 
None.gif在从配置文件加载集合时,会调用该方法以创建各个元素。
None.gif重写该方法以创建特定类型的自定义 ConfigurationElement 对象。 
None.gif
None.gif
2 . protected   abstract   object  GetElementKey(ConfigurationElement element) 
None.gif在派生类中重写时获取指定配置元素的键值。 
None.gif

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值