这里一共写了三种配置方法,第一种是EL的写法,第二种是ELQuickStart的写法,第三种是一般的写法
第一种配置写法
调用处:
///Found Domain and Title in the CooperationChannelsData.xml
ChannelConfig channelConfigs = MasterPageHelper.ListChannelProperty();
-------------------------------------------------------------------
调用方法:
SearchDomain
GetCookieProperty
SplitString
-------------------------------------------------------------------
//************************************************************************************
// 功能: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
Property
}
public class ChannelConfigs
{
Init
Property
}
}
-------------------------------------------------------------------
配置文件:
<?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>
-------------------------------------------------------------------
///Found Domain and Title in the CooperationChannelsData.xml
ChannelConfig channelConfigs = MasterPageHelper.ListChannelProperty();
-------------------------------------------------------------------
调用方法:
SearchDomain
GetCookieProperty
SplitString
-------------------------------------------------------------------
//************************************************************************************
// 功能: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
Property
}
public class ChannelConfigs
{
Init
Property
}
}
-------------------------------------------------------------------
配置文件:
<?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>
----------------------------------------------------------------------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------------------------------------------------------------------
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的节点段中的所有字段都可以取得了。
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)
在派生类中重写时获取指定配置元素的键值。
1.protected abstract ConfigurationElement CreateNewElement()
在从配置文件加载集合时,会调用该方法以创建各个元素。
重写该方法以创建特定类型的自定义 ConfigurationElement 对象。
2.protected abstract object GetElementKey(ConfigurationElement element)
在派生类中重写时获取指定配置元素的键值。