zoukankan      html  css  js  c++  java
  • [原创]自定义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
  • 相关阅读:
    C# 使用HttpWebRequest Post提交数据,携带Cookie和相关参数示例
    关于vue的页面跳转后,如何每次进入页面时都能获取后台数据
    关于vue的页面跳转后,如何每次进入页面时都能获取后台数据
    C#根据汉字获取编码和根据编码获取汉字
    C#根据汉字获取编码和根据编码获取汉字
    多线程系列教材 (一)- Java 创建一个线程的三种方式
    Lambda系列教材 (三)- java 集合的聚合操作
    Lambda系列教材 (二)- 方法引用
    Lambda系列教材 (一)- Java Lambda 表达式教程
    泛型系列教材 (四)- Java 中的子类泛型 转型 成父类泛型
  • 原文地址:https://www.cnblogs.com/RuiLei/p/672424.html
Copyright © 2011-2022 走看看