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
元素。
http://www.4guysfromrolla.com/articles/032807-1.aspx – 2013-05-07 10:05:00