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();
        }
    }
}

この構成も可能ですか?もしそうなら、私は何を間違っていますか?

構成マネージャーでセクションを取得しようとすると、このエラーメッセージが表示されます。

構成エラーの説明:この要求を処理するために必要な構成ファイルの処理中にエラーが発生しました。以下の特定のエラーの詳細を確認し、構成ファイルを適切に変更してください。

パーサーエラーメッセージ:認識されない要素'アセット'。

ソースエラー:

96行目:97行目:98行目:99行目:100行目:

ソースファイル:D:\ ASP.NET Projects \ Resource-Compiler \ ResourceCompiler \ Examples \ web.config行:98

4

3 に答える 3

0

ご覧のとおり、カスタムセクションを「標準」のweb.configファイルに挿入しようとしています。この場合、カスタムセクションをに登録する必要があります

<configSections>

そこに対応するセクションを追加します。たとえば(これは私のプロジェクトの1つにある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

于 2012-05-06T05:25:52.947 に答える
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ファイル用の最も高速で用途の広いシリアライザーです。

于 2012-05-06T06:39:56.533 に答える
0

私はそれを機能させることができました。styleSheetノードとscriptノードに2つの新しいコレクションを追加する必要がありました。これが私の完全なコードです:

    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;
        }
    }
}
于 2012-05-07T01:05:47.440 に答える