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

 更新时间:2022年05月09日 10:20:49   作者:springsnow  
这篇文章介绍了ASP.NET配置文件中自定义节点的方法,文中通过示例代码介绍的非常详细。对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下

节处理程序解释并处理 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配置文件中自定义节点的文章就介绍到这了。希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

相关文章

  • asp.net下url传递中文的解决方案

    asp.net下url传递中文的解决方案

    asp.net下url传递中文的解决方案...
    2007-04-04
  • 在ASP.NET MVC下限制同一个IP地址单位时间间隔内的请求次数的解决方法

    在ASP.NET MVC下限制同一个IP地址单位时间间隔内的请求次数的解决方法

    有时候,当用户请求一个Controller下的Action,我们希望,在单位时间间隔内,比如每秒,每分钟,每小时,每天,每星期,限制同一个IP地址对某个Action的请求次数,如何做呢?这篇文章主要介绍了在ASP.NET MVC下限制同一个IP地址单位时间间隔内的请求次数,需要的朋友可以参考下
    2024-01-01
  • ASP.NET Core使用EF创建关系模型

    ASP.NET Core使用EF创建关系模型

    这篇文章介绍了ASP.NET Core使用EF创建关系模型的方法,文中通过示例代码介绍的非常详细。对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2022-04-04
  • .Net Core路由处理的知识点与方法总结

    .Net Core路由处理的知识点与方法总结

    这篇文章主要给大家介绍了关于.Net Core路由处理的知识点与方法的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2021-04-04
  • asp.net显示页面执行时间

    asp.net显示页面执行时间

    Global.asax需要添加的代码。利用我们了解当前页面的运行效率。
    2009-03-03
  • asp.net开发中常见公共捕获异常方式总结(附源码)

    asp.net开发中常见公共捕获异常方式总结(附源码)

    这篇文章主要介绍了asp.net开发中常见公共捕获异常方式总结,结合实例形式较为详细的分析了asp.net捕获异常的相关技巧,并提供了完整的实例代码供读者下载参考,需要的朋友可以参考下
    2015-11-11
  • asp.net使用AJAX实现无刷新分页

    asp.net使用AJAX实现无刷新分页

    AJAX(Asynchronous JavaScript and XML)是一种进行页面局部异步刷新的技术。用AJAX向服务器发送请求和获得服务器返回的数据并且更新到界面中,不是整个页面刷新,而是在页面中使用Js创建XMLHTTPRequest对象来向服务器发出请求以及获得返回的数据。
    2014-11-11
  • ASP.NET读取RSS的方法

    ASP.NET读取RSS的方法

    这篇文章主要介绍了ASP.NET读取RSS的方法,非常实用的技巧,需要的朋友可以参考下
    2014-08-08
  • ASP.NET Core MVC 过滤器的使用方法介绍

    ASP.NET Core MVC 过滤器的使用方法介绍

    本篇文章主要介绍了ASP.NET Core MVC 过滤器的使用方法介绍,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-09-09
  • .NET事件监听机制的局限与扩展分析

    .NET事件监听机制的局限与扩展分析

    这篇文章主要介绍了.NET事件监听机制的局限与扩展,详细分析了.NET事件监听机制的机制与优劣,有助于更好的理解.NET的运行原理,需要的朋友可以参考下
    2014-11-11

最新评论