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"));
}

最新文章

  1. jQuery静态方法isFunction,isArray,isWindow,isNumeric使用和源码分析
  2. How To Search and Restore files from Site Collection Recycle Bin
  3. vim中执行shell命令
  4. 模拟实现STL中的list
  5. Cisco IOS Basic CLI Configuration:Access Security 01
  6. Codeforces Beta Round #9 (Div. 2 Only)D
  7. BZOJ 1207 打鼹鼠
  8. Spine(2D骨骼动画)
  9. Emmet 语法探析
  10. Java中的嵌套类和内部类
  11. Nginx之旅系列 - Nginx日志功能 PK Linux内核printk
  12. Html基础详解之(jquery)之二
  13. StudyJams学习历程总结
  14. P1822 魔法指纹
  15. 如何让你的数据有null
  16. 解决 ajax 不能下载文件的问题
  17. JS Range使用整理
  18. webpack打包器简单入门
  19. 淘宝开源系统监控工具Tsar
  20. 一、Delphi中Cxgrid表格滚动条粗细设置

热门文章

  1. Ubuntu 常用快捷键
  2. HDU 4307 Contest 1
  3. EditText电话号码格式化输入、删除案例
  4. 多项福利回馈会员,且看Hao123怎样玩转“霸权主义”
  5. hdu1525 Euclid&amp;#39;s Game , 基础博弈
  6. Linux下安装ipython与jupyter
  7. C++提高速度
  8. 51nod 1272 最大距离 O(nlog(n)) , 快排 , 最大连续子串
  9. IOS设备获取崩溃日志的办法
  10. MSSQL读取xml字符串到临时表