2012-05-06 45 views
1

這裏是我的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(); 
     } 
    } 
} 

這是配置甚至可能嗎?如果是這樣,我做錯了什麼?

當我嘗試使用配置管理器獲取該段時,出現此錯誤消息。

Configuration Error Description: An error occurred during the processing of a configuration file required to service this request. Please review the specific error details below and modify your configuration file appropriately.

Parser Error Message: Unrecognized element 'asset'.

Source Error:

Line 96: Line 97: Line 98: Line 99: Line 100:

Source File: D:\ASP.NET Projects\Resource-Compiler\ResourceCompiler\Examples\web.config Line: 98

+1

,你應該看到和使用** [盒帶(http://getcassette.net/)**,而不是做從頭開始......再加上,它支持更多。試試看! – balexandre

回答

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; 
     } 
    } 
} 
0

正如我所看到的,你想插入自定義欄目,以「標準」的web.config文件。 在這種情況下,您需要註冊您的自定義欄目中

<configSections> 

,並添加相應的節還有。例如(這是我的一個項目一些石英配置):

<?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

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"/> 

支持配置繼承你付出的相當鎖定在serializaton格式,你需要堅持的價格。您可以擴展課程的系統並添加您自己的配置提供程序,但這不是一件容易的任務。至少比使用XmlSerializer這類真正的序列化程序要複雜得多,因爲它在數據格式上擁有最大的自由度。

DataContractSerializer也很好,但它不允許你控制一切。 XmlSerializer仍然是用於xml文件的最快和最通用的串行器。

相關問題