• [2014-02-19]ConfigurationSection:让web.config配置更有条理



    本文针对新手

    使用Web.config的配置信息,一般都习惯于使用

    ConfigurationManager.AppSettings["ConfigKey"]
    

    当程序不断迭代,开发维护了一段时间之后,是不是发现Web.config文件中的配置信息堆砌了一大堆?

    {% highlight xml %}
















    ...

    {% endhighlight %}

    是不是在引入第三方库的时候,发现他们的配置节很独立很清楚?


    先来看看完成后的配置方式

    {% highlight xml %}




    {% endhighlight %}

    如何读取这种配置信息?

    首先需要写这个类:wUtils.EmailHelperSection,wUtils是命名空间

    {% highlight C# %}
    namespace wUtils
    {
    ///


    /// EmailHelper配置类
    ///

    public sealed class EmailHelperSection : ConfigurationSection
    {
    public EmailHelperSection() { }
    [ConfigurationProperty("Smtp_Host", DefaultValue = "")]
    public string Smtp_Host
    {
    get
    {
    return (string)this["Smtp_Host"];
    }
    set
    {
    this["Smtp_Host"] = value;
    }
    }
    [ConfigurationProperty("Smtp_Account", DefaultValue = "")]
    public string Smtp_Account
    {
    get
    {
    return (string)this["Smtp_Account"];
    }
    set
    {
    this["Smtp_Account"] = value;
    }
    }
    [ConfigurationProperty("Smtp_Pwd", DefaultValue = "")]
    public string Smtp_Pwd
    {
    get
    {
    return (string)this["Smtp_Pwd"];
    }
    set
    {
    this["Smtp_Pwd"] = value;
    }
    }
    }
    }
    {% endhighlight %}

    然后是使用配置信息的方式

    {% highlight C# %}
    EmailHelperSection config = (EmailHelperSection)ConfigurationManager.GetSection("EmailHelperSection");
    string email = config.Smtp_Account;
    string password = config.Smtp_Pwd;
    {% endhighlight %}

    Over

  • 相关阅读:
    Objective-C 和 Core Foundation 对象相互转换
    sql学习笔记(18)-----------数据库创建过程
    JVM虚拟机结构
    自己定义控件-MultipleTextView(自己主动换行、自己主动补齐宽度的排列多个TextView)
    Blue Jeans
    怎样注冊 diskgroup 到集群
    改动购物项图书数量的Ajax处理
    leetcode_Integer to Roman
    Buildroot 龙芯1C支持指南
    Buildroot構建指南--Overview【转】
  • 原文地址:https://www.cnblogs.com/personball/p/7455835.html
Copyright © 2020-2023  润新知