ConfigurationSection - 为什么不识别此元素? [英] ConfigurationSection - Why is this element not being recognized?

查看:92
本文介绍了ConfigurationSection - 为什么不识别此元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我希望能够建模以下配置:

I want to be able to model the following configuration:

<bundles>
    <resource type="script">
         <bundle name="common/services">
             <file path="common/consoleService.js" />
             <file path="common/localStorageService.js" />
             <file path="common/restService.js" />
             <!-- ... More files ... -->
         </bundle>
    </resource>
</bundles>




所以我继续创建以下 ConfigurationSection

internal class BundlesSection : ConfigurationSection
{
     internal const string TAG_NAME = "bundles";

     [ConfigurationProperty(ResourceCollection.TAG_NAME,
                            IsRequired = false,
                            IsDefaultCollection = true)]
     internal ResourceCollection Resources
     {
         get { return this[ResourceCollection.TAG_NAME] as ResourceCollection; }
     }
}


[ConfigurationCollection(typeof(ResourceElement),
    AddItemName = ResourceElement.TAG_NAME)]
internal class ResourceCollection : ConfigurationElementCollection
{
    internal const string TAG_NAME = "";

    protected override ConfigurationElement CreateNewElement()
    {
        return new ResourceElement();
    }

    protected override object GetElementKey(ConfigurationElement element)
    {
        return ((ResourceElement)element).Type;
    }
}


[ConfigurationCollection(typeof(BundleElement),
    AddItemName = BundleElement.TAG_NAME)]
internal class ResourceElement : ConfigurationElementCollection
{
     internal const string TAG_NAME = "resource";
     private const string ATTR_TYPE = "type";

     [ConfigurationProperty(ATTR_TYPE,
                           IsRequired = true,
                           IsKey = true)]
     internal string Type
     {
         get { return this[ATTR_TYPE] as string; }
         set { this[ATTR_TYPE] = value; }
     }

    protected override ConfigurationElement CreateNewElement()
    {
        return new BundleElement();
    }

    protected override object GetElementKey(ConfigurationElement element)
    {
        return ((BundleElement)element).Name;
    }
}


[ConfigurationCollection(typeof(FileElement),
    AddItemName = FileElement.TAG_NAME)]
internal class BundleElement : ConfigurationElementCollection
{
     internal const string TAG_NAME = "bundle";
     private const string ATTR_NAME = "name";

     [ConfigurationProperty(ATTR_NAME,
                           IsRequired = true,
                           IsKey = true)]
     internal string Name
     {
         get { return this[ATTR_NAME] as string; }
         set { this[ATTR_NAME] = value; }
     }

    protected override ConfigurationElement CreateNewElement()
    {
        return new FileElement();
    }

    protected override object GetElementKey(ConfigurationElement element)
    {
        return ((FileElement)element).Path;
    }
}


internal class FileElement : ConfigurationElement
{
    internal const string TAG_NAME = "file";
    private const string ATTR_PATH = "path";

    [ConfigurationProperty(ATTR_PATH,
                           IsRequired = true,
                           IsKey = true)]
    internal string Path
    {
         get { return this[ATTR_PATH] as string; }
         set { this[ATTR_PATH] = value; }
    }
}



虽然一切似乎都没问题,首次加载该部分时出现以下异常:

Although everything seems OK, I am getting the following exception when the section is first loaded:


无法识别的元素'包

Unrecognized element 'bundle'


如您所见, BundleElement.TAG_NAME  是" bundle" ,所以我不知道为什么呢没有得到承认。

As you can see, BundleElement.TAG_NAME is "bundle", so I don't know why it isn't being recognized.


我正在加载配置部分,如下所示:

private BundlesSection LoadSection()
{
    return ConfigurationManager.GetSection(String.Format("static/{0}", BundlesSection.TAG_NAME)) as BundlesSection;
}

我的
还有以下内容 Web.config:


I also have the following in my Web.config:

<configuration>

    <configSections>
        <sectionGroup name="static">
            <section name="bundles" type="XXX" restartOnExternalChanges="true" />
        </sectionGroup>
    </configSections>

    <static>
        <bundles configSource=".\Configuration\Static\Bundles.xml" />
    </static>

</configuration>







推荐答案

您的项目类型是什么?Winform,WPF 或Web项目(Asp.Net)?

What's your project type? Winform, WPF or  Web project(Asp.Net)?

根据您的代码,我不能重现你的问题,我建议你提供一个关于这个问题的简化样本,最好帮助我们解决根本原因。或者将项目上传到OneDrive,我们将进一步研究。

Based on your code, I cannot reproduce your issue, I would suggest you provide a simplified sample about this issue, It would be better to help us to figure out the root cause. Or upload your project to OneDrive, we'll take a further look.

顺便说一句,如果您的项目是Asp.Net,请在Asp.Net论坛中重新发布此问题。

By the way, if your project is Asp.Net, please repost this issue in Asp.Net forum.

ASP.NET:
http://forums.asp.net

祝你好运,

Kristin


这篇关于ConfigurationSection - 为什么不识别此元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆