我创建了一个自定义配置部分,如下所示
<configSections> </configSections> <Tabs> <Tab name="Dashboard" visibility="true" /> <Tab name="VirtualMachineRequest" visibility="true" /> <Tab name="SoftwareRequest" visibility="true" /> </Tabs>
自定义配置节处理程序
namespace EDaaS.Web.Helper { public class CustomConfigurationHandler : ConfigurationSection { [ConfigurationProperty("visibility",DefaultValue = "true",Isrequired = false)] public Boolean Visibility { get { return (Boolean)this["visibility"]; } set { this["visibility"] = value; } } } }
运行应用程序时抛出异常无法识别的配置部分选项卡.如何解决这个问题
解决方法
您需要编写一个
configuration handler
来解析此自定义部分.然后在配置文件中注册此自定义处理程序:
<configSections> <section name="mySection" type="MyNamespace.MySection,MyAssembly" /> </configSections> <mySection> <Tabs> <Tab name="one" visibility="true"/> <Tab name="two" visibility="true"/> </Tabs> </mySection>
现在让我们定义相应的配置部分:
public class MySection : ConfigurationSection { [ConfigurationProperty("Tabs",Options = ConfigurationPropertyOptions.Isrequired)] public TabsCollection Tabs { get { return (TabsCollection)this["Tabs"]; } } } [ConfigurationCollection(typeof(TabElement),AddItemName = "Tab")] public class TabsCollection : ConfigurationElementCollection { protected override ConfigurationElement CreateNewElement() { return new TabElement(); } protected override object GetElementKey(ConfigurationElement element) { if (element == null) { throw new ArgumentNullException("element"); } return ((TabElement)element).Name; } } public class TabElement : ConfigurationElement { [ConfigurationProperty("name",Isrequired = true,IsKey = true)] public string Name { get { return (string)base["name"]; } } [ConfigurationProperty("visibility")] public bool Visibility { get { return (bool)base["visibility"]; } } }
现在您可以访问设置:
var mySection = (MySection)ConfigurationManager.GetSection("mySection");