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

时间:2013-05-07 09:51:07

标签: c# asp.net web-config

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

这是我的配置:

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

我只想阅读自定义模板中ViewFilter下的所有ViewFilterGroup元素。

1 个答案:

答案 0 :(得分:0)

你缺少的东西很少。

首先,我们需要一个与您的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();
        }
    }
}