- - PR -
Web.configに追加したカスタムセクションを読み込む方法
1
投稿者 | 投稿内容 |
---|---|
|
投稿日時: 2008-01-28 01:57
こんばんは。
たかと申します。 つたない文章ですが、 どうかよろしくお願いします。 【システム条件】 VS2005 ASP.NETでC#を使用しています。 【やりたいこと】 Web.configに以下のようにカスタムセクションを追加しました。 下記URLのように、Microsoftで紹介されているやり方で読み込みたいと思います。 方法 : ConfigurationSection を使用してカスタム構成セクションを作成する http://msdn2.microsoft.com/ja-jp/library/2tw134k3(vs.80).aspx 読み込みたいカスタムセクションの構成は以下のとおりです。 備考: <customSections>と<mySection>がひとつで、 <action>と<forward>が複数。 ---Web.config---------------------- <configSections> <section name="customSections" type="ActionMappingHandler"/> </configSections> <customSections> <actionMapping> <action path="path1" type="type1" name="name1"> <forward name ="name1" /> <forward name ="name2" /> <forward name ="name3" /> </action> <action path="path2" type="type2" name="name2"> <forward name ="name1" /> <forward name ="name2" /> <forward name ="name3" /> </action> </actionMapping> </customSections> ----------------------------- 【わからないこと】 上記でforward部分が無い場合の読み込みはできたのですが、 forwardを追加した場合に、読み込みができません。 下記のソースで、 ActionMappingHandler ActionElementCollection ActionElement(public ForwardElementCollection ForwardCollection部分を消去) だけを記述した場合には、forwardを無しで読み込めました。 forward用に、ForwardElementCollection,ForwardElementを追加したら、 認識されない要素 'forward' です。 ConfigurationErrorsExceptionが発生しました。 という例外が発生するようになりました。 どのようにすれば、Forwardも読み込めるようになるのかがわかりません。 【ソース】 ■ActionMappingHandler public class ActionMappingHandler : ConfigurationSection { [ConfigurationProperty("actionMapping")] public ActionElementCollection ActionCollection { get { ActionElementCollection fileConfigurationSource = (ActionElementCollection)this["actionMapping"]; return fileConfigurationSource; } } } ■ActionElementCollection public class ActionElementCollection : ConfigurationElementCollection { public ActionElementCollection() { this.AddElementName = "action"; } public ActionElement Get(int index) { return (ActionElement)base.BaseGet(index); } public ActionElement Get(string name) { return BaseGet(name) as ActionElement; } public bool Contains(string name) { return BaseGet(name) != null; } protected override ConfigurationElement CreateNewElement() { return new ActionElement(); } protected override object GetElementKey(ConfigurationElement element) { ActionElement childElement = element as ActionElement; return childElement.Path; } } ■ActionElement public class ActionElement : ConfigurationElement { [ConfigurationProperty("path", IsRequired = true, IsKey = true)] public string Path { get { return (string)this["path"]; } set { this["path"] = value; } } [ConfigurationProperty("type", IsRequired = true)] public string Type { get { return (string)this["type"]; } set { this["type"] = value; } } [ConfigurationProperty("name", IsRequired = true)] public string Name { get { return (string)this["name"]; } set { this["name"] = value; } } [ConfigurationProperty("action")] public ForwardElementCollection ForwardCollection { get { ForwardElementCollection fileConfigurationSource = (ForwardElementCollection)this["action"]; return fileConfigurationSource; } } } ■ForwardElementCollection public class ForwardElementCollection : ConfigurationElementCollection { public ForwardElementCollection() { this.AddElementName = "forward"; } public ForwardElement Get(int index) { return (ForwardElement)base.BaseGet(index); } public ForwardElement Get(string name) { return BaseGet(name) as ForwardElement; } public bool Contains(string name) { return BaseGet(name) != null; } protected override ConfigurationElement CreateNewElement() { return new ForwardElement(); } protected override object GetElementKey(ConfigurationElement element) { ForwardElement childElement = element as ForwardElement; return childElement.Name; } } ■ForwardElement public class ForwardElement : ConfigurationElement { [ConfigurationProperty("name", IsRequired = true, IsKey = true)] public string Name { get { return (string)this["name"]; } set { this["name"] = value; } } } 投稿後追記コメント: なぜか投稿すると、インデントが消えてしまいます。 ソース読みにくくてごめんなさい。 [ メッセージ編集済み 編集者: taka 編集日時 2008-01-28 01:59 ] |
1