首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >自定义Web配置集合需要一双全新的眼睛

自定义Web配置集合需要一双全新的眼睛
EN

Stack Overflow用户
提问于 2013-02-19 08:23:36
回答 1查看 77关注 0票数 0

我已经关注这个太久了。我知道我只是在做一些愚蠢的事情,但我看不出来。在我添加异常之前,我的集合总是空的。在添加之后,它总是被抛出。我遗漏了什么?

我有一个web配置集合,看起来像这样:

代码语言:javascript
复制
 <section name="WorkOutGroups" type="System.Configuration.WorkOutGroupsSection"/>

<WorkoutGroups>
    <Workouts>
        <add url="something.asp" />
        <add url="/subfolder/another.asp" />
        <add url="../default.asp" />
    </Workouts>
</WorkoutGroups>

我的类看起来像这样:

代码语言:javascript
复制
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using System.Collections;

namespace MySite.Configuration
{
    public class WorkOutsGroupsSection : ConfigurationSection
    {
        [ConfigurationProperty("WorkOut", IsDefaultCollection = false)]
        [ConfigurationCollection(typeof(WorkOutCollection))]
        public WorkOutCollection WorkOut
        {
            get
            {
                var WorkOut = (WorkOutCollection)base["WorkOut"];
                return WorkOut;
            }
        }
    }

    public class WorkOutCollection : ConfigurationElementCollection
    {
        protected override ConfigurationElement CreateNewElement()
        {
            return new WorkOutElement();
        }

        protected override object GetElementKey(ConfigurationElement element)
        {
            object result = null;
            if (element is WorkOutElement)
            {
                result = ((WorkOutElement)element).WorkOut;
            }
            return result;
        }

        public WorkOutElement this[int index]
        {
            get { return (WorkOutElement)BaseGet(index); }
        }
    }

    public class WorkOutElement : ConfigurationElement
    {
        [ConfigurationProperty("url", IsRequired = true, IsKey = true)]
        public string WorkOut
        {
            get
            {
                return (string)this["url"];
            }
            set
            {
                this["url"] = value;
            }
        }
    }

    public static class WorkOutsGroups
    {
        static WorkOutsGroupsSection WorkOutsGroupsSection
        {
            get
            {
                var section = ConfigurationManager.GetSection("WorkOutsGroups")
                    as WorkOutsGroupsSection;
                if (section != null)
                {
                    return section;
                }
                throw new ConfigurationErrorsException("WorkOutsGroups Configuration not found");
            }
        }

        public static IEnumerable<string> GetWorkOut()
        {
            var results = WorkOutsGroupsSection.WorkOut
                     .OfType<WorkOutElement>()
                     .Select(p => p.WorkOut);

            return results.ToArray();
        }
    }
}

我像这样实现它:

代码语言:javascript
复制
foreach (var url in Workout.GetWorkout())
{
    if (link.Value.Contains(url.ToString()))
        returnValue = true;
}
EN

回答 1

Stack Overflow用户

发布于 2013-02-19 20:38:55

拼写:

代码语言:javascript
复制
ConfigurationManager.GetSection("WorkOutsGroups")

节名称为WorkOutGroups而不是WorkOutsGroups

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/14947753

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档