2013-05-07 68 views
1

我想通過useragent和regex過濾一些瀏覽器,因此將過濾規則放在web.config中似乎是一個很好的解決方案。
但我不知道如何讀取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 = "[email protected]#$%^&*()[]{}/;'\"|\\", 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; } 
    } 
} 

我只是想讀我的自定義moudle ViewFilterGroup下的所有ViewFilter元素。

+1

http://www.4guysfromrolla.com/articles/032807-1.aspx – 2013-05-07 10:05:00

回答

0

你很想念的東西。

首先,我們需要對應於您的ViewFilter一類,我們要對所有的屬性轉移到那裏,是這樣的:

public class ViewFilterElement : ConfigurationElement 
{ 
    [ConfigurationProperty("name", DefaultValue = "", IsRequired = true)] 
    [StringValidator(InvalidCharacters = "[email protected]#$%^&*()[]{}/;'\"|\\", 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(); 
     } 
    } 
} 
相關問題