https://msdn.microsoft.com/en-us/library/2tw134k3.aspx

You can extend ASP.NET configuration settings with XML configuration elements of your own.

To do this, you create a custom configuration section handler.

The handler must be a .NET Framework class that inherits from the System.Configuration.ConfigurationSection class.

The section handler interprets解释 and processes the settings that are defined in XML configuration elements in a specific section of a Web.config file.

You can read and write these settings through the handler's properties.

To create a custom configuration section handler

  1. Create a public class that inherits from the System.Configuration.ConfigurationSection class.

  2. Add code to define the section's attributes and elements.

The following example shows how to create a handler for a custom configuration section named "PageAppearance."

The section has a RemoteOnly attribute and Font and Color elements.

The code shows how to use string, integer, and Boolean attributes.

It also shows how to use string and integer validators验证器 for those attributes. (The Boolean attribute is automatically validated经过验证的.)

The validators check the format of the configuration markup at run time and throw exceptions if the values provided for the custom attributes do not meet the specified criteria.

This example uses the declarative model.

A configuration section handler can also be implemented programmatically.

For an example, see Classes Used to Create Custom Section Handlers and the System.Configuration.ConfigurationSection class overview.

To add a custom section handler to an ASP.NET configuration file

1.In the Web.config file, add a sectionGroup element and a section element inside the configSections element, as shown in the following example.

The declaration associates the custom section handler with the section name.

Note:Nesting a section element in a sectionGroup is optional, but we recommend doing this to help organize configuration data.

<configuration>
<!-- Configuration section-handler declaration area. -->
<configSections>
<sectionGroup name="pageAppearanceGroup">
<section
name="pageAppearance"
type="Samples.AspNet.PageAppearanceSection"
allowLocation="true"
allowDefinition="Everywhere"
/>
</sectionGroup>
<!-- Other <section> and <sectionGroup> elements. -->
</configSections> <!-- Configuration section settings area. --> </configuration>

You can add the section-handler declaration in a different configuration file than the one where you add the custom configuration elements,

providing that the configuration file where the section handler is declared is higher in the configuration file hierarchy.

If you add the section handler declaration to a configuration file that is outside of your application, you must do the following:

  • Include the assembly that defines the section in the same directory as the Web.config file.

  • Ensure that the type attribute of the section element matches the manifest of the assembly (ensure that you specify both the correct namespace and type name).

If either of these conditions is not fulfilled, a configuration error will be thrown.

For more information, see ASP.NET Configuration File Hierarchy and Inheritance.

2.Add custom configuration elements in the configuration section settings area of the Web.config file, as shown in the following example:

<configuration>

<!-- Configuration section-handler declaration area. -->

  <!-- Configuration section settings area. -->
<pageAppearanceGroup>
<pageAppearance remoteOnly="true">
<font name="TimesNewRoman" size="18"/>
<color background="000000" foreground="FFFFFF"/>
</pageAppearance>
</pageAppearanceGroup> <!-- Other configuration settings, such as system.web --> </configuration>

To programmatically access custom configuration data

Get an instance of the custom configuration object and use the GetSection method or the GetSection method to populate it.

The following example shows an ASP.NET Web page that works with the previous examples to enumerate the attributes and child elements of the custom configuration section.

<%@ Page Language="C#" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">
protected void Page_Load(object sender, EventArgs e)
{
Samples.AspNet.PageAppearanceSection config =
(Samples.AspNet.PageAppearanceSection)System.Configuration.ConfigurationManager.GetSection(
"pageAppearanceGroup/pageAppearance"); Response.Write("<h2>Settings in the PageAppearance Section:</h2>");
Response.Write(string.Format("RemoteOnly: {0}<br>",
config.RemoteOnly));
Response.Write(string.Format("Font name and size: {0} {1}<br>",
config.Font.Name, config.Font.Size));
Response.Write(
string.Format("Background and foreground color: {0} {1}<br>",
config.Color.Background, config.Color.Foreground));
}
</script> <html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Custom Configuration Section Example</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<h1>
</div>
</form>
</body>
</html>

class derived from ConfigurationSection Class

https://docs.microsoft.com/en-us/dotnet/api/system.configuration.configurationsection?view=netframework-4.7.2

<runtime> Element

https://docs.microsoft.com/en-us/dotnet/framework/configure-apps/file-schema/runtime/runtime-element

How to programatically modify assemblyBinding in app.config?

https://stackoverflow.com/questions/809262/how-to-programatically-modify-assemblybinding-in-app-config

I found what I needed. The XmlNamespaceManager is required as the assemblyBinding node contains the xmlns attribute. I modified the code to use this and it works:

 private void SetRuntimeBinding(string path, string value)
{
XmlDocument doc = new XmlDocument(); try
{
doc.Load(Path.Combine(path, "MyApp.exe.config"));
}
catch (FileNotFoundException)
{
return;
} XmlNamespaceManager manager = new XmlNamespaceManager(doc.NameTable);
manager.AddNamespace("bindings", "urn:schemas-microsoft-com:asm.v1"); XmlNode root = doc.DocumentElement; XmlNode node = root.SelectSingleNode("//bindings:bindingRedirect", manager); if (node == null)
{
throw (new Exception("Invalid Configuration File"));
} node = node.SelectSingleNode("@newVersion"); if (node == null)
{
throw (new Exception("Invalid Configuration File"));
} node.Value = value; doc.Save(Path.Combine(path, "MyApp.exe.config"));
}

How to: Create Custom Configuration Sections Using ConfigurationSection的更多相关文章

  1. Custom Configuration 的两种方法:1.Configuration Sections

    第一种Configuration Sections 1.App.config 2.CustomConfigurationManager.cs 3.TestProgram.cs. App.config ...

  2. How to Create Custom Filters in AngularJs

    http://www.codeproject.com/Tips/829025/How-to-Create-Custom-Filters-in-AngularJs Introduction Filter ...

  3. create custom launcher icon 细节介绍

    create custom launcher icon 是创建你的Android app的图标 点击下一步的时候,出现的界面就是创建你的Android的图标 Foreground: ” Foregro ...

  4. javax.validation.ValidationException: Unable to create a Configuration, because no Bean Validation provider could be found. Add a provider like Hibernate Validator (RI) to your classpath.

    项目依赖 <dependency> <groupId>javax</groupId> <artifactId>javaee-api</artifa ...

  5. [转]How to Create Custom Filters in AngularJs

    本文转自:http://www.codeproject.com/Tips/829025/How-to-Create-Custom-Filters-in-AngularJs Introduction F ...

  6. java中如何创建自定义异常Create Custom Exception

    9.创建自定义异常 Create Custom Exception 马克-to-win:我们可以创建自己的异常:checked或unchecked异常都可以, 规则如前面我们所介绍,反正如果是chec ...

  7. [转]Create Custom Exception Filter in ASP.NET Core

    本文转自:http://www.binaryintellect.net/articles/5df6e275-1148-45a1-a8b3-0ba2c7c9cea1.aspx In my previou ...

  8. [Angular] Create custom validators for formControl and formGroup

    Creating custom validators is easy, just create a class inject AbstractControl. Here is the form we ...

  9. Custom Configuration 的两种方法:2.XmlSerializer XmlAttribute

    第二种:XmlSerializer XmlAttribute 1.CustomConfiguration.xml 2.CustomConfigurationSetting.cs 3.CustomCon ...

随机推荐

  1. hbase的几种访问方式

    Hbase的访问方式 1.Native Java API:最常规和高效的访问方式: 2.HBase Shell:HBase的命令行工具,最简单的接口,适合HBase管理使用: 3.Thrift Gat ...

  2. 洛谷—— P3225 [HNOI2012]矿场搭建

    https://www.luogu.org/problem/show?pid=3225 题目描述 煤矿工地可以看成是由隧道连接挖煤点组成的无向图.为安全起见,希望在工地发生事故时所有挖煤点的工人都能有 ...

  3. ZOJ 3687

    赤裸的带禁区的排列数,不过,难点在于如何用程序来写这个公式了.纠结了好久没想到,看了看别人的博客,用了DFS,实在妙极,比自己最初想用枚举的笨方法高明许多啊.\ http://blog.csdn.ne ...

  4. [React] Unit test a React Render Prop component

    In this lesson, I use Enzyme and Jest to unit test a Counter Render Prop component. Writing integrat ...

  5. objective-c訪问控制符

    objective-c中成员变量的四个訪问控制符: @private:仅仅有当前类的内部才干訪问 @public:全部人都可訪问 @protected:仅仅限当前类和它的子类可以訪问 @package ...

  6. [cocos2dx笔记013]一个使用CCRenderTexture创建动态纹理显示数字的类

    用CCLabelTTF显示的数字不好看.于是就想到用图片来代理.眼下网上的实现都是把每一个数字做一个CCSprite组合的方式. 可是我想.动态生成纹理的方式.没有就仅仅好自己手动写一个. 头文件 # ...

  7. Linux网络编程(3)——多进程、多线程

    在我的里面已经介绍了linux以下c的进程.线程接口,这里就不做过多阐述了. 多进程 这里多进程採用传统的多进程模型.每当有client发来的连接时创建一个进程来处理连接,一个子进程相应一个连接. 有 ...

  8. c3p0在spring中的配置

    在大家的开发和学习其中应该经经常使用到数据库的连接和使用,只是连接 的方式就有非常多种方式了,例如说用最最简单的JDBC 也好,还实用比 较复杂一点的就是数据库连接池.当然还有使用DBCP的连接的,各 ...

  9. web前端project师知识汇总

    分类: Web开发应用  一.何为Web前端project师?           前端project师,也叫Web前端开发project师.他是随着web发展.细分出来的行业.Web前端开发proj ...

  10. C#上传文件

    QQ:1187362408 欢迎技术交流和学习 关于C#上传文件(产品开发): TODO: 1.文件大小不足500M(web.config配置直接处理) 2,文件大小超过500M(ASP.NET分段读 ...