自定义webconfig

搞了半点的功夫,终于将web.config文件搞清楚,至于web.config中原有的节点配置网上资料很多,先前我也搞了几篇在BLOG上,现在我想要写的是怎么样自定义一个配置节,我在网上找了这方面的资料,大多都是说要怎么做,却都没有给出一个完整的实例,特别是关于创建自定义处理类.

也许你会问,为什么要自定义配置节,有什么用,有没有必要,在什么地方会用到呢?
web.config 这个文件是干么用的:应用程序配置文件包含应用程序特定的设置。该文件包含公共语言运行库读取的配置设置(如程序集绑定策略、远程处理对象等等),以及应用程序可以读取的设置.总之一句话就是你可以通过在WEB.CONFIG文件添加节点来为当前整个应用程序所调用,其中节点的值,有点像全局变量.

1:
要想自已实现自定义配置节,先在web.config文件的
<configuration></configuration>
之间加上<configSections><section name="mysection" type="mywebconfig.myconfig, webconfig"/> </configSections>
其中name="mysection" . 是你想要的节点名称,
type="mywebconfig.myconfig, webconfig"/中mywebconfig.myconfig是实现这个节点处理类的类名,webconfig是mywebconfig.myconfig所在的程序集的名称,比如实现这个节点的处理类所在的程序集名称为A,那么这里就用A代替(这个应注意.MSDN也没有说这个是什么意思,让我在那边试了好久才明了)

2:
为这个节点添加元素
<configuration></configuration>中添加
<mysection>
<add key="key1" value="value"/>
</mysection>
其中mysection是第一步中<configSections><section name="mysection" type="mywebconfig.myconfig, webconfig"/> </configSections>中的name="mysection"的mysection ,应对应.你可以在这边<mysection></mysection>添加任何多的值

3:自定义处理类
在第一步中type="mywebconfig.myconfig, webconfig",我们定义了myconfig来处理这个节点,接下我们自已来写处理这个节点的类,这个类必需继承接口 IConfigurationSectionHandler(在System.Configuration;),并实现这个接口的唯一的一个方法 Create,你可以在这个方法中进行任何操作.
下面是实现的整个代码,我会在代码中做些注解

using  System;
using  System.Xml;
using  System.Configuration;
using  System.Collections.Specialized;
namespace  mywebconfig
{
    
///   <summary>
    
///  myconfig 的摘要说明。
    
///   </summary>

     public   class  myconfig:System.Configuration.IConfigurationSectionHandler     {        
        
        
public  myconfig()
        
{
            
//
            
//  TODO: 在此处添加构造函数逻辑
            
//
        }
        
///   <summary>
        
///  获取一个NameValueCollection类型的的一个的对应的值
        
///   </summary>
        
///   <param name="albumSettings"> 个NameValueCollection类型 </param>
        
///   <param name="key"> 所在的键 </param>
        
///   <param name="defaultValue"> 默认值 </param>
        
///   <returns></returns>
         private   static   string  ReadSettings(NameValueCollection albumSettings,  string  key,  string  defaultValue)
        
{
            
try
            
{
                
object  albumSetting     =  albumSettings[key];
                
if (albumSetting  ==   null )
                
{
                    
return  defaultValue;
                }

                
else
                
{
                    
return  ( string )albumSetting;
                }

            }

            
catch
            
{
                
return  defaultValue;
            }

        }

///   <summary>
///  
///   </summary>
///   <param name="parent"></param>
///   <param name="configContext"></param>
///   <param name="section"></param>
///   <returns></returns>
         public   object  Create( object  parent,  object  configContext, XmlNode section)
        
{
            myclass mc
= new  myclass();    
            NameValueCollection albumSettings;
            
// NameValueCollection表示可通过键或索引访问的关联 String 键和 String 值的已排序集合。            
            NameValueSectionHandler settingsHandler     =   new  NameValueSectionHandler();
            
// 提供配置节中的名称/值对配置信息。
            albumSettings     =  (NameValueCollection)settingsHandler.Create(parent, configContext, section);
            mc.other
= ReadSettings(albumSettings, " myadd " , " =other " );
            
return  mc;
        }


    }

    
public   class  myclass {
        
public    string  myapp = " webconfig " ;
        
public    string  myname = " kf " ;
        
public    string  myage = " 23 " ;
        
public    string  other = " df " ;
        
public  myclass() {}
    
    }

}


在web.config中添加

< configSections >
 
< section name = " mysection "  type = " mywebconfig.myconfig,webconfig "   />  
    
</ configSections >
  
< mysection >
        
        
< add key = " myadd "  value = " 加一个 " ></ add >
</ mysection >

在..aspx页面调用

mywebconfig.myclass myc = new  myclass();
            myc
= ConfigurationSettings.GetConfig( " mysection " as  myclass;
            
this .TextBox1.Text = myc.myapp;
            
this .TextBox2.Text = myc.myname;
            
this .TextBox3.Text = myc.myage;
            
this .TextBox4.Text = myc.other;
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值