gpt4 book ai didi

asp.net - 无法识别的配置部分

转载 作者:行者123 更新时间:2023-12-03 14:51:22 25 4
gpt4 key购买 nike

我创建了一个自定义配置部分,如下所示

<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");

关于asp.net - 无法识别的配置部分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13985914/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com