GetSection方法读取的是configSections节点,这个节点在web.config配置文件中,它比较特殊,必须放置于首节点,也就是说,在它之前不能有其它类型的节点。configSections子节点有section和sectionGroup,后者是前者的集合节点:

<configSections>
<section name="CustomConfig" type="OrderMvc.CustomConfig, OrderMvc"/>
</configSections>

web.config关于CustomConfig的定义:

  <CustomConfig>
<Name Value="asdf"/>
</CustomConfig>

下面说说它的作用,通过对ConfigurationManager.GetSection(...)方法的调用,如果某个类继承IConfigurationSectionHandler接口,那么会触发此接口的Create方法,这样我们就可以做一些事了。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Configuration;
using System.Xml; namespace OrderMvc
{
public class CustomConfig : IConfigurationSectionHandler
{
public string Name { get; private set; }
public object Create(object parent, object configContext, XmlNode section)
{
CustomConfig config = new CustomConfig();
var name = section.SelectSingleNode("Name");
if (name != null && name.Attributes != null)
{
var attribute = name.Attributes["Value"];
if (attribute != null)
config.Name = attribute.Value;
}
return config;
}
}
}

调用:

var config = ConfigurationManager.GetSection("CustomConfig") as CustomConfig;
Debug.WriteLine(config.Name);

ConfigurationManager.GetSection()方法的使用的更多相关文章

  1. configurationmanager.getsection usage example.

    1.app.config(note that attribute case sensitive!) <?xml version="1.0" encoding="ut ...

  2. ConfigurationManager.AppSettings方法

    一 配置文件概述: 应用程序配置文件是标准的 XML 文件,XML 标记和属性是区分大小写的.它是可以按需要更改的,开发人员可以使用配置文件来更改设置,而不必重编译应用程序.配置文件的根节点是conf ...

  3. configurationmanager.getsection usage

    public static void CreateAppSettings() { // Get the application configuration file. System.Configura ...

  4. Url路径重写的原理

    ASP.net的地址重写(URLRewriter)实现原理及代码示例 吴剑 2007-01-01 原创文章,转载必需注明出处:http://www.cnblogs.com/wu-jian/ 概述 访问 ...

  5. C#读取Appconfig中自定义的节点

    今天在使用Nlog的时候,发现了一个之前没注意的问题. 以前,我的app配置文件都是这么写的,当然配置比较多的时候会改用xml. 如果<appSettings>节点中的内容很多的话,我自己 ...

  6. configSections

         由于最近一个项目的数据库变动比较频繁, 为了减少数据层的负担, 打算采用.net的MVC框架, 使用LINQ对付数据层.       这个框架的web.config文件里出现了configS ...

  7. asp.net web.config的学习笔记

    原文地址:http://www.cnblogs.com/Bulid-For-NET/archive/2013/01/11/2856632.html 一直都对web.config不太清楚.这几天趁着项目 ...

  8. configSections(配置文件)

    转载:http://www.cnblogs.com/jhxk/articles/1609182.html 由于最近一个项目的数据库变动比较频繁, 为了减少数据层的负担, 打算采用.net的MVC框架, ...

  9. Common.Logging源码解析一

    Common.Logging是Apache下的一个开源日志接口组件,主要用于切换不同的日志库,因为当前流行的日志库有很多向log4j.log4net(log4j的.net版本)等等,所以为了能灵活的切 ...

随机推荐

  1. Map/Reduce之间的Partitioner接口

    一.Partitioner介绍 Partitioner的作用是对Mapper产生的中间结果进行分片,以便将同一分组的数据交给同一个Reduce处理,它直接影响Reduce阶段的负载均衡(个人理解:就是 ...

  2. 通过gdb跟踪进程调度分析进程切换的过程

    作者:吴乐 山东师范大学 <Linux内核分析>MOOC课程http://mooc.study.163.com/course/USTC-1000029000 本实验目的:通过gdb在lin ...

  3. springMVC(注解版笔记)

    springMVC(注解版) 较之于非注解版本,发生一下变化: 1.配置文件需要配置的标签有: <!-- 包的扫描,此包下面的所有包都启用注解 --> <context:compon ...

  4. 踩刹车——regularization

    从一个问题说起: 当我们使用H10去拟合曲线的时候,其实我们只想要H2的结果.如果从H10变回到H2呢? 所以我们只需要添加上限制条件:w3=...=w10=0即可.现在呢,我们可以放宽一点条件:任意 ...

  5. Winxp下搭建SVN服务器

    本文介绍一种在winxp下搭建SVN服务器的方法. (1) 需要下载Slik-Subversion和TortoiseSVN两个软件.我使用的版本是Slik-Subversion-1.8.3-1-win ...

  6. 在阿里云linux下使用SVN访问VisualSVN出错:SSL handshake failed: SSL error: Key usage violation in certificate has been detected

    Subversion clients receive the following error message when attempting to connect to VisualSVN Serve ...

  7. ACM1995

    /* 汉诺塔V Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Sub ...

  8. [LeetCode] #1# Two Sum : 数组/哈希表/二分查找/双指针

    一. 题目 1. Two SumTotal Accepted: 241484 Total Submissions: 1005339 Difficulty: Easy Given an array of ...

  9. Red5点播和直播的实现

    (一)        Red5流媒体服务器介绍Red5是一个采用Java开发开源的Flash流媒体服务器.它支持:把音频(MP3)和视频(FLV)转换成播放流: 录制客户端播放流(只支持FLV):共享 ...

  10. C++中#include包含头文件带 .h 和不带 .h 的区别

    C++中#include包含头文件带 .h 和不带 .h 的区别? 如 #include <iostream> 和 #include <iostream.h> 包含的东西有哪些 ...