web.config 研究

一、将配置映射成类

1、配置中增加

    <configSections>
        <section name="appConfiguration" type="OilDigital.CGGL.AppConfig.AppConfigurationHandler,OilDigital.CGGL.AppConfig,Culture=neutral, PublicKeyToken=null" />
        
    </configSections>
<appConfiguration>
        <EnterPriseName>中石油</EnterPriseName>
</appConfiguration>

2、类

public class AppConfiguration:System.Configuration.ConfigurationSection
    {
        public AppConfiguration()
        {
            
        }

        /// <summary>
        /// 企业名称
        /// </summary>
        [ConfigurationProperty("EnterPriseName", DefaultValue = "工程建设公司")]
        public string  EnterPriseName
        {
            get { return (string)this["EnterPriseName"]; }
            set { this["EnterPriseName"] = value; }
        }
}

3、使用

AppConfiguration config =(AppConfiguration)System.Configuration.ConfigurationManager.GetSection("appConfiguration");

猜你喜欢

转载自www.cnblogs.com/zhengwei-cq/p/11760486.html