您的位置:首页 > 其它

web.config文件相关(后边的程序看的有点糊涂,但是前面的解释是很正确的)

2008-11-30 09:52 411 查看
搞了半点的功夫,终于将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;
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  string blog null 语言