• 为asp.net程序添加自定义配置区域 (转)


    我们通常把诸如sql的connection string之类的配置信息保存在web.config的AppSettings部分,以方便程序的分发,并且可以通过以下方法在程序中获得:
    string sqlStr = System.Configuration.ConfigurationSettings.AppSettings["ConnectionString"];

    对于结构比较复杂的自定义配置,可以通过实现IConfigurationSectionHandler接口来实现这种机制。首先,创建MySettings类,该类仅包含了我需要的一些自定义配置的定义:

    using System;

    namespace myconfig
    {
        
    public class MySettings
        
    {
            
    public string SomeSetting; //自定义一个string类型的配置信息

            
    public MySettings()
            
    {
            }

        }

    }

    接下来关键的一步就是创建用于处理刚才定义好了的MySettings这类配置的MyConfigHandler,需要实现IConfigurationSectionHandler接口,IConfigurationSectionHandler只有一个方法:

    object Create(
      
    object parent,
       object configContext,
       XmlNode section
     );

    因为web.config文件是一个标准的xml文件,所以可以非常简单得读出其中XmlNode的值:

    using System;
    using System.Configuration;
    using System.Xml;

    namespace myconfig
    {
        
    public class MyConfigHandler: IConfigurationSectionHandler
        
    {
            
    public MyConfigHandler()
            
    {
            }

        
            
    public object Create(object parent, object input, XmlNode node) 
            
    {
                MySettings myset 
    = new MySettings();
                
    foreach (XmlNode n in node.ChildNodes) 
                
    {
                    
    switch (n.Name)
                    
    {
                        
    case("SomeSetting"): //从web.config读取SomeSetting的值
                            myset.SomeSetting = n.InnerText; 
                            
    break;
                        
    default:
                            
    break;
                    }

                }


                
    return myset;

            }


        }

    }

    至此所有的自定义配置类和Handler都已经创建好了,最后只要告诉web.config用MyConfigHandler来处理MySettings就可以了,需要在web.config添加下列内容:

        <configSections>
            
    <section name="MySettings" type="myconfig.MyConfigHandler,myconfig"></section>
        
    </configSections>
        
        
    <MySettings>
            
    <SomeSetting>This is a customer configuration setting.</SomeSetting>
        
    </MySettings>

    其中<configSecions>告诉web.config调用MyConfigHandler来处理MySettings,<MySettings>中保存的就是自定义的配置内容,例如在某个web page中执行如下代码:

            private void Page_Load(object sender, System.EventArgs e)
            
    {
                
    // Put user code to initialize the page here
                MySettings myset;
                myset 
    = System.Configuration.ConfigurationSettings.GetConfig("MySettings"as MySettings;

                Response.Write(myset.SomeSetting);
            }

    得到的结果将会是在客户端显示This is a customer configuration setting。其实还有另一种更简单的方法,就是利用NameValueFileSectionHandler,但是在添加配置信息时需要像在AppSettings中那样用<add name="" value=""></add>来添加键值,对于自定义配置来说意义不大,具体可以参考msdn中相关的文章。

    原文: http://www.cnblogs.com/roger/archive/2004/11/02/59788.aspx

  • 相关阅读:
    greybox关闭/刷新父窗口
    C# 获取文件编码
    框架页,URL中文参数乱码
    用来代替SQLSERVERAGENT的VBS脚本。
    jQuery的radio,checkbox,select操作
    mssql 的sp_help好难看
    如何判断网通、电信、铁通IP地址分配段
    IE8取不到 select 的option值
    如何识别当前的 SQL Server 版本号以及对应的产品级别
    控诉我的电脑
  • 原文地址:https://www.cnblogs.com/dagon007/p/118158.html
Copyright © 2020-2023  润新知