[有关于EL的Configuration Block和自定化配置]
这里一共写了三种配置方法,第一种是EL的写法,第二种是ELQuickStart的写法,第三种是一般的写法
第一种配置写法
调用处:

/**////Found Domain and Title in the CooperationChannelsData.xml
ChannelConfig channelConfigs = MasterPageHelper.ListChannelProperty();

-------------------------------------------------------------------

调用方法:


SearchDomain#region SearchDomain


/**//// <summary>
/// 查找配置文件中的Domain
/// </summary>
public static ChannelProperty SearchDomain()

{
string serverName = HttpContext.Current.Request.ServerVariables["server_name"].ToLower();

ChannelProperty defaultChannel = Channels.DefaultChannel.Get(0);

//这里我们做一个DefaultChannel
if (defaultChannel.Name.Equals(serverName))

{
return defaultChannel;
}
else

{ //做一个其他 Channel
foreach (ChannelProperty data in Channels.Channels)

{
String[] strings = SplitString(data.Name);

foreach (String name in strings)

{
if (name.Trim().Equals(serverName))

{
return data;
}
}
}
}

return defaultChannel;
}


/**//// <summary>
/// 返回处理
/// </summary>
/// <returns></returns>
public static ChannelConfigs ListChannelProperty()

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

GetCookieProperty(configs);

return configs;
}

#endregion


GetCookieProperty#region GetCookieProperty


/**//// <summary>
///
/// </summary>
public static void GetCookieProperty(ChannelConfigs configs)

{
String serverName = GetServerName();

//域名为XXX
if (XXX.Equals(configs.DomainName))

{
if (HttpContext.Current.Request.Cookies[XXX] != null)

{
//Url
configs.HeaderUrl = "www.HeaderUrl.com";
configs.FooterUrl = "www.footerurl.com";
}
}
}

#endregion


SplitString#region SplitString


/**//// <summary>
/// Split String
/// Split Symbol is ":"
/// </summary>
public static String[] SplitString(String src)

{
return src.Split(SPLITSYMBOL);
}

#endregion


-------------------------------------------------------------------

//************************************************************************************
// 功能:ChannelCollection
//
//************************************************************************************
namespace Ctrip.UI.DomesticFlight


{
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;
using System.Configuration;

public class CooperationSettings : SerializableConfigurationSection

{
public const string SectionName = "CooperationChannels";
private const string channelSettingsProperty = "ChannelSettings";
private const string DefaultChannelProperty = "DefaultChannel";

[ConfigurationProperty(channelSettingsProperty, IsRequired = true)]
public NamedElementCollection<ChannelProperty> Channels

{

get
{ return (NamedElementCollection<ChannelProperty>)base[channelSettingsProperty]; }
}

[ConfigurationProperty(DefaultChannelProperty, IsRequired = true)]
public NamedElementCollection<ChannelProperty> DefaultChannel

{

get
{ return (NamedElementCollection<ChannelProperty>)base[DefaultChannelProperty]; }
}
}
}

-------------------------------------------------------------------

//************************************************************************************
// 功能:ConfigurationData
//
//************************************************************************************
namespace XXXX


{
using System;
using System.Text;
using System.Configuration;
using System.Collections.Generic;
using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;
using Microsoft.Practices.EnterpriseLibrary.Common.Configuration.ObjectBuilder;

public class ChannelProperty : NamedConfigurationElement

{

Define Params#region Define Params

private const string builderNameProperty = "builderName";

#endregion


Property#region Property

[ConfigurationProperty(nameProperty, IsKey = true, DefaultValue = "Name", IsRequired = true)]
[StringValidator(MinLength = 1)]
public new string Name

{

get
{ return (string)this[nameProperty]; }

set
{ this[nameProperty] = value; }
}

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

[ConfigurationProperty(builderNameProperty, IsRequired = true)]
public string BuilderName

{

get
{ return (string)base[builderNameProperty]; }

set
{ base[builderNameProperty] = value; }
}

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

#endregion
}

public class ChannelConfigs

{

Init#region Init

public ChannelConfigs(ChannelProperty propertys)

{
name = propertys.Name;
}

#endregion


Property#region Property

private string name;
public string Name

{

get
{ return name; }

set
{ name = value; }
}

#endregion
}
}

-------------------------------------------------------------------

配置文件:

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

-------------------------------------------------------------------
第二种配置方法的写法
1.有关于自定义配置
----------------------------------------------------------------------------------------------------------------------------------------------------
MasterPage:
private void GetRelatedDomainAndSetPageTitle()

{
NameValueCollection channelCollection = new CooperationChannelsConfig().Settings;

foreach (String channel in channelCollection)

{
if (channel.Equals(UIHelper.GetServerName(this.Page)))

{
DomainName = channelCollection[channel].Split('|')[0].ToString();
this.Page.Title = channelCollection[channel].Split('|')[1].ToString();
break;
}
}

//默认域名[hp]下
if (String.IsNullOrEmpty(DomainName) == true)

{
DomainName = "hp";
this.Page.Title = "默认标题
-"; }
}

----------------------------------------------------------------------------------------------------------------------------------------------------

CooperationChannelsConfig :

namespace NameSpace


{
public class CooperationChannelsConfig : IConfigurationSectionHandler

{

/**//// 实现一个类支持IConfigurationSectionHandler 接口来对自定义节进行处理,完成对自定义节的读取
public object Create(object parent, object configContext, XmlNode section)

{
NameValueCollection settings;

try

{
NameValueSectionHandler baseHandler = new NameValueSectionHandler();
settings = (NameValueCollection)baseHandler.Create(parent, configContext, section);
}
catch

{
settings = null;
}

return settings;
}


/**//// <summary>
/// 返回整个Channel
/// </summary>
public NameValueCollection Settings

{
get

{
return (NameValueCollection)ConfigurationManager.GetSection("channel");
}
}
}
}

----------------------------------------------------------------------------------------------------------------------------------------------------

Global:
System.Configuration.ConfigurationManager.GetSection("channel");

----------------------------------------------------------------------------------------------------------------------------------------------------

Web.config
<section name="channel" type="NameSpace.CooperationChannelsConfig, NameSpace" />
<channel configSource="Config\\Channel.config"/>

----------------------------------------------------------------------------------------------------------------------------------------------------

Channel.config:

<?xml version="1.0" encoding="gb2312"?>
<channel>
<add key="gotone.smcc.flights.ctrip.com" value="mc_group|"/>
<!--特商外包-->
<add key="xinhuanet.flights.ctrip.com" value="xinhuanet|新华网"/>
<add key="flights.ctrip.xinhuanet.com" value="ctripxinhuanet|央视国际"/>
<add key="cctv.flights.ctrip.com" value="cctv|"/>
<add key="airchina.flights.ctrip.com" value="airchina|"/>
</channel>

----------------------------------------------------------------------------------------------------------------------------------------------------

第三种配置的写法
配置项目节点属性类:
namespace NameSpace


{
using System.Text;
using System.Configuration;
using String = System.String;


/**//// <summary>
/// ChannelProperty
/// </summary>
public class ChannelProperty : ConfigurationSection

{
[ConfigurationProperty("urlName")]
public String UrlName

{

get
{ return this["urlName"].ToString(); }
}


/**//// <summary>
/// DomainName:crp
/// </summary>
[ConfigurationProperty("domainName")]
public String DomainName

{

get
{ return this["domainName"].ToString(); }
}


/**//// <summary>
/// baseTitle
/// </summary>
[ConfigurationProperty("baseTitle")]
public String BaseTitle

{

get
{ return this["baseTitle"].ToString(); }
}


/**//// <summary>
///
/// </summary>
[ConfigurationProperty("cssSrc")]
public String CssSrc

{

get
{ return this["cssSrc"].ToString(); }
}
}
}
----------------------------------------------------------------------------------------------

namespace NameSpace


{
using System;
using System.Collections.Generic;
using System.Text;
using System.Configuration;

[ConfigurationCollection(typeof(ChannelProperty))]
public sealed class ChannelCollection : ConfigurationElementCollection

{
protected override ConfigurationElement CreateNewElement()

{
return new ChannelProperty();
}

protected override object GetElementKey(ConfigurationElement element)

{
return ((ChannelProperty)element).UrlName;
}
}

public sealed class CooperationChannels : ConfigurationSection

{
[ConfigurationProperty("ChannelSettings", IsDefaultCollection = false)]
[ConfigurationCollection(typeof(ChannelCollection), AddItemName = "Channel")]
public ChannelCollection Channels

{
get

{
return base["ChannelSettings"] as ChannelCollection;
}
}
}
}

----------------------------------------------------------------------------------------------

Channel.Config
<?xml version="1.0" encoding="gb2312"?>
<CooperationChannels>
<ChannelSettings>
<Channel domainName="baidu" urlName="www.baidu.com" baseTitle="hp-" cssSrc="" />
<Channel domainName="hp" urlName="www.hp.com" baseTitle="hp-" cssSrc="" />
</ChannelSettings>
</CooperationChannels>

----------------------------------------------------------------------------------------------

Global文件:
应添加,去读取Web.Config中的节点
protected void Application_Start(object sender, EventArgs e)

{ System.Configuration.ConfigurationManager.GetSection("CooperationChannels");
}

----------------------------------------------------------------------------------------------

Web.Config

<configSections>
<section name="CooperationChannels" type="Ctrip.UI.DomesticFlight.CooperationChannels, Ctrip.UI.DomesticFlight" />
</configSections>

<CooperationChannels configSource="Config\\Channels.config"/>

----------------------------------------------------------------------------------------------

MasterPage页面调用处:
public partial class MasterPage_DA : System.Web.UI.MasterPage

{
ChannelProperty configData = null;

CooperationChannels channelSections = ConfigurationManager.GetSection("CooperationChannels") as CooperationChannels;
foreach (ChannelProperty data in channelSections.Channels)

{
if (data.UrlName.Equals("www.baidu.com"))

{
configData = data;
}
}
}

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

最后还是补充一下:
1.protected abstract ConfigurationElement CreateNewElement()
在从配置文件加载集合时,会调用该方法以创建各个元素。
重写该方法以创建特定类型的自定义 ConfigurationElement 对象。

2.protected abstract object GetElementKey(ConfigurationElement element)
在派生类中重写时获取指定配置元素的键值。
