如何从web.config中读取自定义节下的所有元素

本文关键字:元素 自定义 读取 web config | 更新日期: 2023-09-27 18:12:30

我想通过useragent和regex过滤一些浏览器,所以把过滤规则放在web中。Config似乎是个不错的解决方案。
但是我不知道如何从web.config中读取自定义部分下的所有元素。


<configuration>
    <system.webServer>
    <modules>
        <add name="ViewFilterModule" type="ViewFilterModule"/>
    </modules>
    <configSections>
        <sectionGroup name="ViewFilterGroup">
          <section 
            name="ViewFilter" 
            type="CMG.ViewFilter" 
            allowLocation="true" 
            allowDefinition="Everywhere"
          />
        </sectionGroup>
    </configSections>
    <ViewFilterGroup>
        <ViewFilter name="ucweb" regex="ucweb" redirect="~/ucweb.html" />
        <ViewFilter name="operamini" regex="opera mini" redirect="~/operamini.html" />
        <ViewFilter name="ucweb" regex="ucweb" redirect="~/ucweb.html" />
        <ViewFilter name="operamini" regex="opera mini" redirect="~/operamini.html" />
        <ViewFilter name="ucweb" regex="ucweb" redirect="~/ucweb.html" />
        <ViewFilter name="operamini" regex="opera mini" redirect="~/operamini.html" />
    </ViewFilterGroup>
    </system.webServer>
</configuration>

这是我的代码:

public class ViewFilterSection : ConfigurationSection
{
    [ConfigurationProperty("name", DefaultValue = "", IsRequired = true)]
    [StringValidator(InvalidCharacters = "~!@#$%^&*()[]{}/;''"|''", MinLength = 1, MaxLength = 20)]
    public String Name
    {
        get
        {
            return (String)this["name"];
        }
        set
        {
            this["name"] = value;
        }
    }
    [ConfigurationProperty("regex", DefaultValue = "", IsRequired = false)]
    public String Regex
    {
        get
        { return (String)this["regex"]; }
        set
        { this["regex"] = value; }
    }
    [ConfigurationProperty("redirect", DefaultValue = "", IsRequired = false)]
    public String Redirect
    {
        get
        { return (String)this["redirect"]; }
        set
        { this["redirect"] = value; }
    }
}

我只想在自定义模块中读取ViewFilterGroup下的所有ViewFilter元素。

如何从web.config中读取自定义节下的所有元素

你错过了几件事。

首先,我们需要一个对应于你的ViewFilter的类我们将把你所有的属性移到那里,比如:

public class ViewFilterElement : ConfigurationElement
{
    [ConfigurationProperty("name", DefaultValue = "", IsRequired = true)]
    [StringValidator(InvalidCharacters = "~!@#$%^&*()[]{}/;''"|''", MinLength = 1, MaxLength = 20)]
    public String Name
    {
        get { return (String)this["name"]; } set { this["name"] = value; }
    }
    [ConfigurationProperty("regex", DefaultValue = "", IsRequired = false)]
    public String Regex
    {
        get { return (String)this["regex"]; } set { this["regex"] = value; }
    }
    [ConfigurationProperty("redirect", DefaultValue = "", IsRequired = false)]
    public String Redirect
    {
        get { return (String)this["redirect"]; } set { this["redirect"] = value; }
    }
}

然后有一个ConfigurationElementCollection属性对应于你的ViewFilterGroup:

public class ViewFilterCollection : ConfigurationElementCollection
{
    protected override ConfigurationElement CreateNewElement()
    {
        return new ViewFilterElement();
    }
    protected override object GetElementKey(ConfigurationElement element)
    {
        return ((ViewFilterElement)element).Name;
    }
}

然后在你的ViewFilterSection类,你可以有一个属性,返回一个列表在你的新的ViewFilterElement类型,像这样

public class ViewFilterSection : ConfigurationSection
{
    [ConfigurationProperty("", IsRequired = true, IsDefaultCollection = true)]
    [ConfigurationCollection(typeof(ViewFilterCollection), AddItemName = "ViewFilter")]
    private ViewFilterCollection ViewFilterCollection
    {
        get { return (ViewFilterCollection)this[string.Empty]; }
        set { this[string.Empty] = value; }
    }
    public static List<ViewFilterElement> ViewFilters
    {
        get
        {
            ViewFilterSection sec = (ViewFilterSection)ConfigurationManager.GetSection("ViewFilterGroup");
            return sec.ViewFilterCollection.As<ViewFilterElement>().ToList();
        }
    }
}