惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Stack Overflow Blog
Stack Overflow Blog
T
Tailwind CSS Blog
Recent Announcements
Recent Announcements
宝玉的分享
宝玉的分享
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
P
Proofpoint News Feed
D
Docker
Google DeepMind News
Google DeepMind News
aimingoo的专栏
aimingoo的专栏
B
Blog RSS Feed
Microsoft Security Blog
Microsoft Security Blog
博客园 - 【当耐特】
M
MIT News - Artificial intelligence
云风的 BLOG
云风的 BLOG
小众软件
小众软件
Hugging Face - Blog
Hugging Face - Blog
WordPress大学
WordPress大学
IT之家
IT之家
H
Help Net Security
Apple Machine Learning Research
Apple Machine Learning Research
Martin Fowler
Martin Fowler
S
SegmentFault 最新的问题
B
Blog
D
DataBreaches.Net

博客园 - NET峰

Bind和Eval的区别详解 常用WebService一览表 信息系统集成分析师高级资格考试 js日期格式化 - NET峰 - 博客园 利用正则表达式实现字符串搜索 玩转.net正则表达式 弹出输入框 c#.net常用函数和方法集 解决 500 Internal Server Error 搜索引擎优化(SEO) Report Server 报表服务器安装sp2补丁后,报表服务器不能用的问题解决方法 asp.net 从excel 导入时 提示:不是预期的格式错误 解决方法 javascript操作表格 示例源码 自己动手写控件----textbox之实现视图状态! 《msdn开发精选》2005年全部源代码! 报表制作利器—Reporting Services 使用总结! 简单的XML结合XSL的例子,有助于学习内部的原理(内附源码) XMLHTTP详解 C# 格式化字符串 String.Format (推荐)
创建web.config自定义配置部分
NET峰 · 2006-09-09 · via 博客园 - NET峰

     今天看了一下web.config创建自定义配置部分,虽然大多数常用的应用程序专用的设置可以保存在appsettings元素中,但有时还是需要添加自定义的配置元素,增强可用的配置元素集.即是我们用不到,分析一个自定义的配置部分还是对我们更好的理解如何解析web.config有好处的.
      以下举例说明,假设我们希望应用程序能够用acme元素指定自定义元素,acme元素包含在一个新的称为acmegroup的部分中,比如有一下元素:font,backgroundColor,underlineLinks,horizontalWidth,verticalWidth.
webconfig代码如下:

<configuration>

  <configSections>

    <sectionGroup name="acmeGroup">

  <section name="acme"

          type="EssentialAspDotNet.Config.AcmeConfigHandler, AcmeConfigHandler"

  />

    </sectionGroup>

  </configSections>

  <myGroup>

    <add key="font" value="Courier New"/>

    <add key="backgroundColor" value="Green"/>

    <add key="underlineLinks" value="true"/>

    <add key="horizontalWidth" value="600"/>

    <add key="verticalWidth" value="800"/>

  </myGroup>

  <acmeGroup>

    <acme>

  <font>Courier New</font>

  <backgroundColor>Green</backgroundColor>

      <underlineLinks>true</underlineLinks>

      <horizontalWidth>600</horizontalWidth>

      <verticalWidth>800</verticalWidth>

</acme>

  </acmeGroup>

  <appSettings>

    <add key="DSN"

         value="server=localhost;uid=sa;pwd=;database=pubs"

    />

    <add key="bgColor" value="blue" />

  </appSettings>

</configuration>

 自定义配置设置

//AcmeSettings.cs

using System;

namespace EssentialAspDotNet.Config
{
public class AcmeSettings
{
public string Font;
public string BackgroundColor;
public bool UnderlineLinks;
public int HorizontalWidth;
public int VerticalWidth;

public override string ToString()
{
return string.Format("AcmeSettings: Font={0}, BackgroundColor={1}, UnderlineLinks={2}, HorizontalWidth={3}, VerticalWidth={4}",
Font, BackgroundColor, UnderlineLinks, HorizontalWidth, VerticalWidth);
}
}
}

自定义配置部分处理程序
//AcmeConfigHandler.cs
using System;using System.Xml;using System.Configuration;using System.Web;using System.Web.Configuration;namespace EssentialAspDotNet.Config

{

public class AcmeConfigHandler : IConfigurationSectionHandler

  {

public object Create(object parent, object input, XmlNode node)

    {

      AcmeSettings aset =

new AcmeSettings();foreach (XmlNode n in node.ChildNodes)

      {

switch (n.Name)

        {

case ("font"):

            aset.Font = n.InnerText;

break;case ("backgroundColor"):

            aset.BackgroundColor = n.InnerText;

break;case ("underlineLinks"):

            aset.UnderlineLinks = bool.Parse(n.InnerText);

break;case ("horizontalWidth"):

            aset.HorizontalWidth = int.Parse(n.InnerText);

break;case ("verticalWidth"):

            aset.VerticalWidth = int.Parse(n.InnerText);

break;

        }

      }

return aset;

    }

  }

}

未完待续:访问自定义配置信息,使用NameValueFileSectionHandler