• [原创]自定义Config例子,使用IConfigurationSectionHandler 接口


    1.MasterPage
      调用方法
     
    private void GetConfig() 
            

                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
                    }
     
                }
     
           }
     

    2.CooperationChannelsConfig : 
    //实现一个类支持IConfigurationSectionHandler 接口来对自定义节进行处理,完成对自定义节的读取
    namespace Config 

        
    using System; 
        
    using System.Data; 
        
    using System.Configuration; 
        
    using System.Web; 
        
    using System.Web.Security; 
        
    using System.Web.UI; 
        
    using System.Web.UI.WebControls; 
        
    using System.Web.UI.WebControls.WebParts; 
        
    using System.Web.UI.HtmlControls; 
        
    using System.Collections.Specialized; 
        
    using System.Xml; 

        
    public class CooperationChannelsConfig : 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"); 
                }
     
            }
     
        }
     
    }
     

    3.在项目中建立一个Global文件,在Application_Start()事件中增加一句话取得GetSection:
    System.Configuration.ConfigurationManager.GetSection(
    "channel"); 

    4.在Web.config中配置一下mapping类和只明使用的自定义的config文件:
        
    <section name="channel" type="Config.CooperationChannelsConfig, Config " /> 
      
    <channel configSource="Config\\Channel.config"/> 

    5.Channel.config: 自定义的Config文件

    <?xml version="1.0" encoding="gb2312"?> 
    <channel> 
      
    <add key="Key" value="value"/> 
      
    <add key="Key1" value="value1"/> 
    </channel> 


    最后我也提供一下MSDN上面的自定义配置节的例子:
    ms-help://MS.MSDNQTR.v80.chs/MS.MSDN.v80/MS.VisualStudio.v80.chs/dv_aspnetconfig/html/07f68a3f-2920-4665-a824-47bda744e662.htm
  • 相关阅读:
    Android将TAB选项卡放在屏幕底部(转)
    unix进程间通信
    C优先级顺序(转)
    C/C++ 内存补齐机制
    Android Sqlite ORM 工具
    类型安全性测试
    反射手册笔记 2.程序集,对象和类型
    CLR笔记:15.委托
    反射手册笔记 4.创建对象
    反射手册笔记 1.灵活的编程方法
  • 原文地址:https://www.cnblogs.com/RuiLei/p/672424.html
Copyright © 2020-2023  润新知