是否可以进行此自定义配置?

时间:2012-05-06 04:58:56

标签: c# asp.net configuration

这是我的xml结构:

<reco>
    <styleSheets>
      <group>
        <asset source="~/Script/file1.css"/>
        <asset source="~/Script/file2.css"/>
        <asset source="~/Script/file3.css"/>
    </group>
  </styleSheets>
  <scripts>
    <group>
        <asset source="~/Content/file1.js"/>
        <asset source="~/Content/file1.js"/>
        <asset source="~/Content/file1.js"/>
    </group>
  </scripts>

这是我的代码:

public class AssetConfigurationElement : ConfigurationElement
{

    /// <summary>
    /// Gets or sets the source.
    /// </summary>
    /// <value>The source.</value>
    [ConfigurationProperty("source", IsRequired = true, IsKey = true)]
    public string Source
    {
        get
        {
            return (string)this["source"];
        }

        set
        {
            this["source"] = value;
        }
    }
}

public class GroupConfigurationElementCollection : ConfigurationElementCollection
{
    public GroupConfigurationElementCollection()
    {
        AddElementName = "asset";
    }

    /// <summary>
    /// Gets or sets the name.
    /// </summary>
    /// <value>The name.</value>
    [ConfigurationProperty("name", IsRequired = true, IsKey = true, IsDefaultCollection = true)]
    public string Name
    {
        get
        {
            return (string)this["name"];
        }

        set
        {
            this["name"] = value;
        }
    }

    /// <summary>
    /// Gets or sets a value indicating whether this <see cref="WebAssetGroupConfigurationElement"/> is enabled.
    /// </summary>
    /// <value><c>true</c> if enabled; otherwise, <c>false</c>.</value>
    [ConfigurationProperty("enabled", DefaultValue = true)]
    public bool Enabled
    {
        get
        {
            return (bool)this["enabled"];
        }

        set
        {
            this["enabled"] = value;
        }
    }

    /// <summary>
    /// Gets or sets the version.
    /// </summary>
    /// <value>The version.</value>
    [ConfigurationProperty("version")]
    public string Version
    {
        get
        {
            return (string)this["version"];
        }

        set
        {
            this["version"] = value;
        }
    }

    /// <summary>
    /// Gets or sets a value indicating whether this <see cref="WebAssetGroupConfigurationElement"/> is compress.
    /// </summary>
    /// <value><c>true</c> if compress; otherwise, <c>false</c>.</value>
    [ConfigurationProperty("compress", DefaultValue = true)]
    public bool Compress
    {
        get
        {
            return (bool)this["compress"];
        }

        set
        {
            this["compress"] = value;
        }
    }

    /// <summary>
    /// Gets or sets a value indicating whether this <see cref="WebAssetGroupConfigurationElement"/> is combined.
    /// </summary>
    /// <value><c>true</c> if combined; otherwise, <c>false</c>.</value>
    [ConfigurationProperty("combined", DefaultValue = true)]
    public bool Combined
    {
        get
        {
            return (bool)this["combined"];
        }

        set
        {
            this["combined"] = value;
        }
    }


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

    protected override object GetElementKey(ConfigurationElement element)
    {
        return ((AssetConfigurationElement)element).Source;
    }

}

public class SharedGroupConfigurationSection : ConfigurationSection
{

    /// <summary>
    /// Gets the style sheets.
    /// </summary>
    /// <value>The style sheets.</value>
    [ConfigurationProperty("styleSheets")]
    [ConfigurationCollection(typeof(GroupConfigurationElementCollection), AddItemName = "group")]
    public GroupConfigurationElementCollection StyleSheets
    {
        get
        {
            return (GroupConfigurationElementCollection)base["styleSheets"] ?? new GroupConfigurationElementCollection();
        }
    }

    /// <summary>
    /// Gets the style sheets.
    /// </summary>
    /// <value>The style sheets.</value>
    [ConfigurationProperty("scripts")]
    [ConfigurationCollection(typeof(GroupConfigurationElementCollection), AddItemName = "group")]
    public GroupConfigurationElementCollection Scripts
    {
        get
        {
            return (GroupConfigurationElementCollection)base["scripts"] ?? new GroupConfigurationElementCollection();
        }
    }
}

这种配置是否可行?如果是这样,我做错了什么?

当我尝试使用配置管理器获取该部分时,我收到此错误消息。

  

配置错误   描述:处理为此请求提供服务所需的配置文件时发生错误。请查看下面的具体错误详细信息并适当修改配置文件。

     

分析程序错误消息:无法识别的元素“资产”。

     

来源错误:

     

第96行:   第97行:   第98行:   第99行:   第100行:

     

源文件:D:\ ASP.NET Projects \ Resource-Compiler \ ResourceCompiler \ Examples \ web.config行:98

3 个答案:

答案 0 :(得分:0)

正如我所看到的,您正在尝试将自定义部分插入“标准”web.config文件。 在这种情况下,您需要在

中注册自定义部分
<configSections>

并在那里添加相应的部分。例如(这是我的一个项目中的一些Quartz配置):

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <configSections>

        <sectionGroup name="common">
            <section name="logging" type="Common.Logging.ConfigurationSectionHandler, Common.Logging" />
        </sectionGroup>
        ... other sections here...
    </configSections>
    ... other web.config stuff here

然后,在下面某处你需要添加你自己的部分:

<common>
    <logging>
        <factoryAdapter type="Common.Logging.Log4Net.Log4NetLoggerFactoryAdapter, Common.Logging.Log4net">
            <arg key="configType" value="INLINE" />
        </factoryAdapter>
    </logging>
</common>

这是关于此主题的msdn文章: http://msdn.microsoft.com/en-us/library/2tw134k3.aspx

答案 1 :(得分:0)

应该是,但每个资产都需要一个名称标签,因为它是每个资产的必需属性。如果你不需要它,那么从中删除IsRequired = true和IsKey = true。 ConfigurationElementCollection确实对App.Config中的数据格式提出了严格的要求。 Sytem.Configuration系统允许您从父app.config文件继承值。这甚至适用于对象集合。为了使其工作,MS设计师引入了特殊标签。

要删除从父app.config加载的集合中的所有元素<clear/> 标签已被添加。

要添加元素,您需要<add/>标记,其中数据被分类到属性中。 您的app.config应该看起来像

<add asset source="~/Script/file1.css"/>

要支持配置继承,您需要支付您需要遵守的完全锁定的序列化格式的价格。您当然可以扩展系统并添加自己的配置提供程序,但这不是一件容易的事。至少比使用像XmlSerializer这样的真正的序列化程序要复杂得多,你可以最自由地使用数据格式。

DataContractSerializer也不错,但它不允许你控制一切。 XmlSerializer仍然是xml文件中速度最快,功能最多的序列化程序。

答案 2 :(得分:0)

我能够让它运转起来。我必须为styleSheet节点和脚本节点添加两个新集合。这是我的完整代码:

    public class SharedGroupConfigurationSection : ConfigurationSection
{

    /// <summary>
    /// Gets the style sheets.
    /// </summary>
    /// <value>The style sheets.</value>
    [ConfigurationProperty("styleSheets", Options = ConfigurationPropertyOptions.IsRequired)]        
    public StyleSheetConfigurationElementCollection StyleSheets
    {
        get
        {
            return (StyleSheetConfigurationElementCollection)base["styleSheets"] ?? new StyleSheetConfigurationElementCollection();
        }
    }

    /// <summary>
    /// Gets the style sheets.
    /// </summary>
    /// <value>The style sheets.</value>
    [ConfigurationProperty("scripts", Options = ConfigurationPropertyOptions.IsRequired)]        
    public ScriptConfigurationElementCollection Scripts
    {
        get
        {
            return (ScriptConfigurationElementCollection)base["scripts"] ?? new ScriptConfigurationElementCollection();
        }
    }
}


[ConfigurationCollection(typeof(GroupConfigurationElementCollection), AddItemName = "group")]
public class ScriptConfigurationElementCollection : ConfigurationElementCollection
{

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

    protected override object GetElementKey(ConfigurationElement element)
    {
        if (element == null)
        {
            throw new ArgumentNullException();
        }

        return ((GroupConfigurationElementCollection)element).Name;
    }
}

[ConfigurationCollection(typeof(GroupConfigurationElementCollection), AddItemName="group")]
public class StyleSheetConfigurationElementCollection : ConfigurationElementCollection
{

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

    protected override object GetElementKey(ConfigurationElement element)
    {
        if (element == null)
        {
            throw new ArgumentNullException();
        }

        return ((GroupConfigurationElementCollection)element).Name;
    }
}

[ConfigurationCollection(typeof(AssetConfigurationElement), AddItemName = "asset", CollectionType = ConfigurationElementCollectionType.BasicMap)]
public class GroupConfigurationElementCollection : ConfigurationElementCollection
{
    public GroupConfigurationElementCollection()
    {
        AddElementName = "asset";
    }

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

    protected override object GetElementKey(ConfigurationElement element)
    {
        if (element == null)
        {
            throw new ArgumentNullException();
        }

        return ((AssetConfigurationElement)element).Source;
    }

    /// <summary>
    /// Gets or sets the name.
    /// </summary>
    /// <value>The name.</value>
    [ConfigurationProperty("name", IsRequired = true)]
    public string Name 
    {
        get
        {
            return (string)base["name"];
        }
    }

    /// <summary>
    /// Gets or sets a value indicating whether this <see cref="WebAssetGroupConfigurationElement"/> is enabled.
    /// </summary>
    /// <value><c>true</c> if enabled; otherwise, <c>false</c>.</value>
    [ConfigurationProperty("enabled", DefaultValue = true)]
    public bool Enabled
    {
        get
        {
            return (bool)this["enabled"];
        }

        set
        {
            this["enabled"] = value;
        }
    }

    /// <summary>
    /// Gets or sets the version.
    /// </summary>
    /// <value>The version.</value>
    [ConfigurationProperty("version")]
    public string Version
    {
        get
        {
            return (string)this["version"];
        }

        set
        {
            this["version"] = value;
        }
    }

    /// <summary>
    /// Gets or sets a value indicating whether this <see cref="WebAssetGroupConfigurationElement"/> is compress.
    /// </summary>
    /// <value><c>true</c> if compress; otherwise, <c>false</c>.</value>
    [ConfigurationProperty("compress", DefaultValue = true)]
    public bool Compress
    {
        get
        {
            return (bool)this["compress"];
        }

        set
        {
            this["compress"] = value;
        }
    }

    /// <summary>
    /// Gets or sets a value indicating whether this <see cref="WebAssetGroupConfigurationElement"/> is combined.
    /// </summary>
    /// <value><c>true</c> if combined; otherwise, <c>false</c>.</value>
    [ConfigurationProperty("combined", DefaultValue = true)]
    public bool Combined
    {
        get
        {
            return (bool)this["combined"];
        }

        set
        {
            this["combined"] = value;
        }
    }
}

public class AssetConfigurationElement : ConfigurationElement
{

    /// <summary>
    /// Gets or sets the source.
    /// </summary>
    /// <value>The source.</value>
    [ConfigurationProperty("source", IsRequired = false, IsKey = false)]
    public string Source
    {
        get
        {
            return (string)this["source"];
        }

        set
        {
            this["source"] = value;
        }
    }
}