[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

原文地址:https://www.cnblogs.com/personball/p/7455835.html