无法为 web.config 创建自定义部分 [英] Unable to create a custom section for web.config
问题描述
我在 web.config 文件中创建了一个自定义部分,但它无法加载我要管理该部分的自定义类型.
I created a custom section in the web.config file but it isn't able to load my custom type that is going to manage the section.
定义如下:
<configSections>
<section
name="MembershipProviders"
type="MyApp.BusinessObjects.MembershipProviderFactory.MembershipProvidersSection"
allowLocation="true"
allowDefinition="Everywhere"
/>
</configSections>
namespace MyApp.BusinessObjects
{
public class MembershipProviderFactory
{
internal virtual IMembershipProvider Create()
{
}
public class MembershipProvidersSection : ConfigurationSection
{
public class AddElement: ConfigurationElement
{
[ConfigurationProperty("name", IsKey = true, IsRequired = true)]
public string Name
{
get
{
return this["name"].ToString();
}
set
{
this["name"] = value;
}
}
[ConfigurationProperty("type", IsRequired = true)]
public string FullyQualifiedTypeName
{
get
{
return this["type"].ToString();
}
set
{
this["type"] = value;
}
}
}
public class AddElementCollection : ConfigurationElementCollection
{
protected override ConfigurationElement CreateNewElement()
{
return new AddElement();
}
protected override object GetElementKey(ConfigurationElement element)
{
return ((AddElement)element).Name;
}
}
[ConfigurationProperty("currentProvider", IsRequired = false)]
public string CurrentProvider
{
get
{
return this["currentProvider"].ToString();
}
set
{
this["currentProvider"] = value;
}
}
[ConfigurationProperty("add", IsRequired = true, IsDefaultCollection = true)]
public AddElementCollection Instances
{
get { return (AddElementCollection)this["add"]; }
set { this["add"] = value; }
}
}
}
}
我收到一个运行时异常,内容为:
I get a run-time exception that says:
为 MembershipProviders 创建配置节处理程序时出错:无法加载类型MyApp.BusinessObjects.MembershipProviderFactory.MembershipProvidersSection".
更新
我还在配置文件中包含了实际部分,如下所示:
I also included the actual section in the config file as follows:
<MembershipProviders currentProvider ="DefaultMembershipProvider" />
我仍然遇到同样的异常.
I still get the same exception.
推荐答案
您需要将程序集名称指定为 type
属性的一部分:
You need to specify the assembly name as part of the type
attribute:
<section
name="MembershipProviders"
type="Namespace.TheCustomSection, TheAssemblyNameGoesHere"
allowLocation="true"
allowDefinition="Everywhere"
/>
编辑
我没有注意到 MembershipProvidersSection
类是一个嵌套类.
类型名称应为:
EDIT
I didn't notice that the MembershipProvidersSection
class is a nested class.
The type name should be:
MyApp.BusinessObjects.MembershipProviderFactory+MembershipProvidersSection
这篇关于无法为 web.config 创建自定义部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!