ASP.NET中的自定义配置文件元素:`属性不是ConfigurationElement'

时间:2015-02-18 19:24:59

标签: c# asp.net web-config

我试图实现从ASP.NET web.config文件中首次阅读自定义配置元素。这对我来说似乎很简单,但是我收到了一个运行时`属性不是一个配置元素'错误。这是我的代码:

web.config文件部分:

<?xml version="1.0" encoding="utf-8" ?>...
<configuration>
  ...
  <section name="appConfig" type="ParticipationManagement.AppConfig" allowDefinition="Everywhere" allowLocation="true" requirePermission="false" />
  </configSections>
  ...
  <appConfig>
    <startRecertVFC>2/15</startRecertVFC>
  </appConfig>
</configuration>

处理程序:

using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Web;

namespace ParticipationManagement
{
    public class AppConfig : ConfigurationSection
    {
        [ConfigurationProperty("startRecertVFC", IsRequired = false)]
        public DateTime RecertVFCStart
        {
            get
            {
                string start = (string)this["startRecertVFC"];
                string year = DateTime.Now.Year.ToString();
                DateTime start_date;

                if (DateTime.TryParse(start + "/" + year, out start_date))
                {
                    return start_date;
                }
                else
                {
                    return DateTime.Today;
                }
            }
            set
            {
                this["startRecertVFC"] = value;
            }
        }
    }
}

我在页面代码中的调用:

        protected void Page_Init(object sender, EventArgs e)
        {
...

            AppConfig config = (AppConfig)System.Configuration.ConfigurationManager.GetSection("appConfig");
            RecertVFCStart = config.RecertVFCStart;

        }

对我来说似乎非常干净和直截了当,但我在运行时遇到了这个令人不安的错误,因为我对此没有经验,所以我无法缩小它。

提前抱歉:我看到很多关于此的帖子,但似乎都解决了比我想要完成的更高级/复杂的问题,这只不过是在一个特定的应用程序中嵌入了一些应用程序特定的值。外部文件...

1 个答案:

答案 0 :(得分:-2)

  1. 更改您的配置设置,如下所示

    <?xml version="1.0"?>
    <configuration>
    <configSections>
     <section name="appConfig" type="ParticipationManagement.AppConfig" allowDefinition="Everywhere" allowLocation="true" requirePermission="false"/>
    </configSections>        
    <appConfig startRecertVFC="5/1/2015 8:30:52 AM"  />
    </configuration>
    
  2. 尝试在AppConfig文件中进行以下更改

    string start = this [“startRecertVFC”]。ToString();

  3. 如果这可以解决您的问题,请告诉我们