簡體   English   中英

可以進行此自定義配置嗎?

[英]Is it possible to make this custom 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

如我所見,您正在嘗試將自定義部分插入“標准” 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/zh-cn/library/2tw134k3.aspx

應該是,但是您需要為每個資產添加一個名稱標簽,因為它是每個資產的必需屬性。 如果不需要它,則從中刪除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這樣的真正的序列化器要復雜得多,在XserSerializer中,您對數據格式的自由度最大。

DataContractSerializer也很好,但是它不允許您控制所有內容。 XmlSerializer仍然是xml文件最快,最通用的序列化程序。

我能夠使它工作。 我必須為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;
        }
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM