iis服务器助手广告广告
返回顶部
首页 > 资讯 > 后端开发 > ASP.NET >ASP.NET配置文件中自定义节点
  • 706
分享到

ASP.NET配置文件中自定义节点

2024-04-02 19:04:59 706人浏览 独家记忆
摘要

节处理程序解释并处理 WEB.config 文件特定部分中 XML 配置元素中定义的设置,并根据配置设置返回适当的配置对象。 处理程序类返回的配置对象可以是任何数据结构;它不限于任何

节处理程序解释并处理 WEB.config 文件特定部分中 XML 配置元素中定义的设置,并根据配置设置返回适当的配置对象。

处理程序类返回的配置对象可以是任何数据结构;它不限于任何基配置类或配置格式。

asp.net 使用该配置对象,以对自定义配置元素进行读取和写入。

1、创建自定义配置节处理程序

创建一个继承 System.Configuration.ConfigurationSection 类的公共类

public class MyHandler : ConfigurationSection
    {
        public MyHandler()
        {
        }

        public MyHandler(String attribVal)
        {
            MyAttrib1 = attribVal;
        }

    [ConfigurationProperty("myAttrib1", DefaultValue = "Clowns", IsRequired = true)]
    [StringValidator(InvalidCharacters = "~!@#$%^&*()[]{}/;'\"|\\", MinLength = 1, MaxLength = 60)]
    public String MyAttrib1
    {
        get
        { return (String)this["myAttrib1"]; }
        set
        { this["myAttrib1"] = value; }
    }

    [ConfigurationProperty("myChildSection")]
    public MyChildConfigElement MyChildSection
    {
        get
        { return (MyChildConfigElement)this["myChildSection"]; }
        set
        { this["myChildSection"] = value; }
    }
}

public class MyChildConfigElement : ConfigurationElement
{
    public MyChildConfigElement()
    {
    }

    public MyChildConfigElement(String a1, String a2)
    {
        MyChildAttribute1 = a1;
        MyChildAttribute2 = a2;
    }

    [ConfigurationProperty("myChildAttrib1", DefaultValue = "Zippy", IsRequired = true)]
    [StringValidator(InvalidCharacters = "~!@#$%^&*()[]{}/;'\"|\\", MinLength = 1, MaxLength = 60)]
    public String MyChildAttribute1
    {
        get
        { return (String)this["myChildAttrib1"]; }
        set
        { this["myChildAttrib1"] = value; }
    }

    [ConfigurationProperty("myChildAttrib2", DefaultValue = "Michael Zawondy", IsRequired = true)]
    [StringValidator(InvalidCharacters = "~!@#$%^&*()[]{}/;'\"|\\", MinLength = 1, MaxLength = 60)]
    public String MyChildAttribute2
    {
        get
        { return (String)this["myChildAttrib2"]; }
        set
        { this["myChildAttrib2"] = value; }
    }
}

2、向 ASP.net 配置文件添加自定义节处理程序

<configuration>

  <configSections>
    <sectionGroup name="myCustomGroup">
      <section 
        name="myCustomSection" 
        type="MyConfigSectionHandler.MyHandler, MyCustomConfigurationHandler, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" 
        allowLocation="true" 
        allowDefinition="Everywhere"
      />
    </sectionGroup>
  </configSections>

  <myCustomGroup>
    <myCustomSection myAttrib1="Clowns">
      <myChildSection 
          myChildAttrib1="Zippy" 
          myChildAttrib2="Michael Zawondy "/>
    </myCustomSection>
  </myCustomGroup>

</configuration>

3、以编程方式访问自定义配置数据

获取自定义配置对象的一个实例,并使用 GetSection 方法或 GetSection 方法来填充该实例。

下面的 ASPX 页的示例使用前一个示例,以枚举自定义配置节的属性和子元素。

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

<script runat="server">
    protected void Button1_Click(object sender, EventArgs e)
    {
        MyConfigSectionHandler.MyHandler config =
            (MyConfigSectionHandler.MyHandler)System.Configuration.ConfigurationManager.GetSection(
            "myCustomGroup/myCustomSection");
        
        StringBuilder sb = new StringBuilder();

        sb.Append("<h2>Attributes in the myCustomSection Element:</h2>");
        sb.AppendFORMat("myAttrib1 = {0}<br/>", config.MyAttrib1.ToString());
        sb.Append("<h2>Attributes in the myChildSection Element:</h2>");
        sb.AppendFormat("myChildAttrib1 = {0}<br/>", config.MyChildSection.MyChildAttribute1.ToString());
        sb.AppendFormat("myChildAttrib2 = {0}<br/>", config.MyChildSection.MyChildAttribute2.ToString());

        Label1.Text = sb.ToString();
        Label1.Visible = true;
    }
</script>

<html xmlns="Http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    
    <h1>Enumerate MyCustomSection</h1>
    <asp:Label ID="Label1" runat="server" 
        Text="" />
    <br />
    <asp:Button ID="Button1" runat="server" 
        Text="Get Custom Config Info" 
        OnClick="Button1_Click" />

    </div>
    </form>
</body>
</html>

到此这篇关于ASP.NET配置文件中自定义节点的文章就介绍到这了。希望对大家的学习有所帮助,也希望大家多多支持编程网。

--结束END--

本文标题: ASP.NET配置文件中自定义节点

本文链接: https://www.lsjlt.com/news/148275.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作