`

Using SingleTagSectionHandler Instead Of appSettings--转载

阅读更多
<script type="text/javascript"><!----></script><script src="http://pagead2.googlesyndication.com/pagead/show_ads.js" type="text/javascript"> </script>

While working with any kind of configuration files in .NET, be it Web.config in case of ASP.NET or App.config in case of Windows Forms applications, we very often use the appSettings section. We use it to store all kinds of simple configuration options. Options, that are too simple for us to implement a completely new SectionHandler type. There is however one problem with appSettings section not very expressive.

What does it mean to be expressive? Consider the following example:

<appSettings>
  <add key="source1user" value="user" />
<add key="source1password" value="pass" /> appSettings>

In simple cases, it may be acceptable, but what happens if more user keys are required for some reason? Maybe we need to access few different locations, each of which requires a username and password? We may use some kind of prefix for each key like in the above example, but that is not very elegant. What is key and value anyway?

Another obvious problem here is when we need to have 2 parameters associated with a single logical functionality in the application, we need 2 entries in appSettings section.

If we still don't want to implement a new SectionHandler type, we have very nice option left: SingleTagSectionHandler.

MSDN describes it as: "Handles configuration sections that are represented by a single XML tag in the .config file". And that's about it. Unfortunately, (as usual) MSDN provides no example of how to use it. Fortunately it is quite simple:

<configSections>
  <section name="remoteDataSource" type="System.Configuration.SingleTagSectionHandler" />
configSections>

<remoteDataSource username="user" password="pass" url="http://remote/" />

Using the newly declared section from the code is also easy:

Hashtable remoteDataSource = 
(Hashtable)WebConfigurationManager.GetSection("remoteDataSource");
string username = (string)remoteDataSource["username"];
string password = (string)remoteDataSource["password"];
string url = (string)remoteDataSource["url"];

Simple, yet useful.

评论

相关推荐

    asp.net 读取配置文件方法

    这里使用了`ConfigurationSettings.AppSettings`属性来访问`appSettings`节中的配置信息。 #### 五、自定义配置文件 自定义配置文件是指除了预定义的`appSettings`之外,还可以根据需要定义新的配置节。 ##### 5.1...

    C_读取配置文件详解教程

    在C#中,读取`appSettings`节的配置信息非常简单,可以通过`ConfigurationSettings`类的静态属性`AppSettings`来访问。例如,获取连接字符串可以这样做: ```csharp string connectionString = Configuration...

    C#配置文件概述[文].pdf

    - `type`:指示处理配置节数据的类,有几种预定义的类型,如`System.Configuration.SingleTagSectionHandler`、`System.Configuration.DictionarySectionHandler`和`System.Configuration.NameValueSectionHandler`...

    自定义应用程序配置文件(app.config)

    - `type`: 指定自定义配置节的类型,常见的有`System.Configuration.SingleTagSectionHandler`、`System.Configuration.DictionarySectionHandler`和`System.Configuration.NameValueSectionHandler`。 接下来展示...

    C#读取配置文件详解教程

    声明配置节可以使用 section 节点,例如 `&lt;section name="Test1" type="System.Configuration.SingleTagSectionHandler"/&gt;`。设置配置节可以使用自定义的配置节,例如 `&lt;Test1&gt;...&lt;/Test1&gt;`。 在 C# 中,读取自定义...

    C# 配置文件概述

    下面是一个最常见的应用程序配置文件的例子 只有appSettings 节 &lt; xml version &quot;1 0&quot; encoding &quot;utf 8&quot; &gt; &lt;configuration&gt; &lt;appSettings&gt; &lt;add key &quot;...

Global site tag (gtag.js) - Google Analytics