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
  • 相关阅读:
    更改默认alert框体
    自定义垂直拖动的seekbar进度条
    android适配pad和部分手机底部虚拟按键+沉浸式状态栏
    解决studio的URI is not registered (Setting|Language&Frameworks|Schemas and DTDs)
    王者荣耀是怎样炼成的(二)《王者荣耀》unity安装及使用的小白零基础入门
    王者荣耀是怎样炼成的(一)《王者荣耀》用什么开发,游戏入门,unity3D介绍
    使用python(command line)出现的ImportError: No module named 'xxx'问题
    Android Studio生成keystore签名文件步骤讲解
    greendao数据库初次使用的配置及多表关联的初始化
    android视频双向实时通讯的横竖屏切换
  • 原文地址:https://www.cnblogs.com/RuiLei/p/672424.html
Copyright © 2011-2022 走看看