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

C# 是否可以进行此自定义配置?,c#,asp.net,configuration,C#,Asp.net,Configuration,以下是我的xml结构: <reco> <styleSheets> <group> <asset source="~/Script/file1.css"/> <asset source="~/Script/file2.css"/> <asset source="~/Script/file3.css"/> </group> </st

以下是我的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();
        }
    }
}
公共类资产配置元素:配置元素
{
/// 
///获取或设置源。
/// 
///消息来源。
[ConfigurationProperty(“源”,IsRequired=true,IsKey=true)]
公共字符串源
{
得到
{
返回(字符串)此[“源”];
}
设置
{
此[“源”]=值;
}
}
}
公共类GroupConfigurationElementCollection:ConfigurationElementCollection
{
公共组配置元素集合()
{
addlementname=“资产”;
}
/// 
///获取或设置名称。
/// 
///名字。
[ConfigurationProperty(“名称”,IsRequested=true,IsKey=true,IsDefaultCollection=true)]
公共字符串名
{
得到
{
返回(字符串)此[“名称”];
}
设置
{
此[“名称”]=值;
}
}
/// 
///获取或设置一个值,该值指示是否已启用。
/// 
///如果启用,则为true;否则为false。
[ConfigurationProperty(“已启用”,DefaultValue=true)]
公共布尔启用
{
得到
{
返回(bool)此[“已启用”];
}
设置
{
此[“已启用”]=值;
}
}
/// 
///获取或设置版本。
/// 
///版本。
[配置属性(“版本”)]
公共字符串版本
{
得到
{
返回(字符串)此[“版本”];
}
设置
{
此[“版本”]=值;
}
}
/// 
///获取或设置一个值,该值指示这是否为压缩。
/// 
///如果压缩,则为true;否则为false。
[ConfigurationProperty(“压缩”,DefaultValue=true)]
公共图书馆
{
得到
{
返回(bool)此[“压缩”];
}
设置
{
此[“压缩”]=值;
}
}
/// 
///获取或设置一个值,该值指示是否合并。
/// 
///如果合并,则为true;否则为false。
[ConfigurationProperty(“组合”,DefaultValue=true)]
公共布尔合并
{
得到
{
返回(bool)此[“合并”];
}
设置
{
此[“组合”]=值;
}
}
受保护的覆盖ConfigurationElement CreateNewElement()
{
返回新的AssetConfiguration元素();
}
受保护的覆盖对象GetElementKey(ConfigurationElement元素)
{
返回((AssetConfiguration元素)元素);
}
}
公共类SharedGroupConfigurationSection:ConfigurationSection
{
/// 
///获取样式表。
/// 
///样式表。
[配置属性(“样式表”)]
[ConfigurationCollection(typeof(GroupConfigurationElementCollection),AddItemName=“group”)]
公共组配置元素集合样式表
{
得到
{
返回(GroupConfigurationElementCollection)基[“样式表”]??新建GroupConfigurationElementCollection();
}
}
/// 
///获取样式表。
/// 
///样式表。
[配置属性(“脚本”)]
[ConfigurationCollection(typeof(GroupConfigurationElementCollection),AddItemName=“group”)]
公共组配置元素集合脚本
{
得到
{
返回(GroupConfigurationElementCollection)基[“脚本”]??新建GroupConfigurationElementCollection();
}
}
}
这种配置可能吗?如果是,我做错了什么

当我尝试使用configuration manager获取节时,会收到此错误消息

配置错误 描述:处理服务此请求所需的配置文件时出错。请查看下面的特定错误详细信息,并适当修改配置文件

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

源错误:

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

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


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

<configSections>

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


... 这里的其他部分。。。
... 这里还有其他web.config的东西
然后,您需要在下面的某个地方添加您自己的部分,如下所示:

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


应该是,但您需要为每个资产设置一个名称标签,因为它是每个资产的必需属性。如果不需要此选项,请从中删除IsRequired=true和IsKey=true。在App.Config中对数据的格式有严格要求。配置系统允许您从父app.config文件继承值。这甚至适用于对象集合。为了使其正常工作,MS designer引入了特殊的标签

要从父app.config加载的集合中删除所有元素,请执行
添加了标签

要添加元素,您需要
标记,将数据序列化到属性中。 然后,您的app.config应该如下所示

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

为了支持配置继承,您需要付出代价,使用您需要遵守的非常锁定的序列化格式。当然,您可以扩展系统并添加自己的配置提供程序,但这不是一项容易的任务。至少更多
<add asset source="~/Script/file1.css"/>
    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;
        }
    }
}