首页 新闻 会员 周边

求助,在实现自定义section配置中遇到配置错误的提示,求原因解答

0
[已关闭问题] 关闭于 2015-03-18 22:01

求助,在实现自定义section配置中遇到配置错误的提示,提示 “无法识别的特性“FileName”。请注意特性名称区分大小写。”求问题解决的方法和问题的原因。

下面是代码:

配置:<section name="CustomerSection" type="App002_MVC_Classes.CustomerSection, App002_MVC_Classes, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" allowDefinition="Everywhere" allowExeDefinition="MachineToApplication" restartOnExternalChanges="true"/>

<CustomerSection FileName="default.txt" MaxUsers="1000" MaxIdleTime="00:15:00" />

代码:

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

namespace App002_MVC_Classes
{
    public sealed class CustomerSection : ConfigurationSection
    {
        private static ConfigurationPropertyCollection _Properties;
        private static bool _ReadOnly;
        private static readonly ConfigurationProperty _FileName =
            new ConfigurationProperty("fileName", typeof(string), "default.txt", ConfigurationPropertyOptions.IsRequired);
        private static readonly ConfigurationProperty _MaxUsers =
            new ConfigurationProperty("maxUsers", typeof(long), (long)1000, ConfigurationPropertyOptions.None);
        private static readonly ConfigurationProperty _MaxIdleTime =
            new ConfigurationProperty("maxIdleTime", typeof(TimeSpan), TimeSpan.FromMinutes(5), ConfigurationPropertyOptions.IsRequired);

        public CustomerSection()
        {
            _Properties = new ConfigurationPropertyCollection();
            _Properties.Add(_FileName);
            _Properties.Add(_MaxUsers);
            _Properties.Add(_MaxIdleTime);
        }

        protected override ConfigurationPropertyCollection Properties
        {
            get
            {
                return base.Properties;
            }
        }

        private new bool IsReadOnly
        {
            get { return _ReadOnly; }
        }

        private void ThrowIfReadly(string propertyName)
        {
            if (IsReadOnly)
                throw new ConfigurationErrorsException("The Property " + propertyName + " is read only.");
        }

        protected override object GetRuntimeObject()
        {
            _ReadOnly = true;
            return base.GetRuntimeObject();
        }

        [StringValidator(InvalidCharacters = "~!@#$%^&*()[]{}/.;\"\\", MinLength = 1, MaxLength = 60)]
        public string FileName
        {
            get {
                return (string)this["fileName"];
            }
            set {
                ThrowIfReadly("fileName");
                this["fileName"] = value;
            }
        }

        [LongValidator(MinValue = 1, MaxValue = 1000000, ExcludeRange = false)]
        public long MaxUsers
        {
            get
            {
                return (long)this["maxUsers"];
            }
            set
            {
                this["maxUsers"] = value;
            }
        }


        [TimeSpanValidator(MinValueString = "0:0:30", MaxValueString = "5:00:0", ExcludeRange = false)]
        public TimeSpan MaxIdleTime
        {
            get
            {
                return (TimeSpan)this["maxIdleTime"];
            }
            set
            {
                this["maxIdleTime"] = value;
            }
        }
    }
}

北在北方的主页 北在北方 | 初学一级 | 园豆:180
提问于:2015-03-18 15:58
< >
分享
所有回答(1)
0

FileName="default.txt"

fileName="default.txt"

Launcher | 园豆:45045 (高人七级) | 2015-03-18 16:01

不起作用

<CustomerSection fileName="default.txt" maxUsers="1000" maxIdleTime="00:15:00" />

支持(0) 反对(0) 北在北方 | 园豆:180 (初学一级) | 2015-03-18 16:05

@北在北方: 

public string fileName
        {
            get {
                return (string)this["fileName"];

支持(0) 反对(0) Launcher | 园豆:45045 (高人七级) | 2015-03-18 16:10

@Launcher: 你在蒙?如果这里需要改成小写,那我两处都是大写跟都是小写应该都一样

这是官网的代码示例:https://msdn.microsoft.com/zh-cn/library/system.configuration.configurationsection.aspx

我是照着官网示例敲的代码

支持(0) 反对(0) 北在北方 | 园豆:180 (初学一级) | 2015-03-18 16:12

@北在北方: 

<?xml version="1.0" encoding="utf-8"?>

<configuration>

 

    <configSections>

<section name="CustomSection" type="Samples.AspNet.  CustomSection, CustomConfigurationSection, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" allowDefinition="Everywhere" allowExeDefinition="MachineToApplication" restartOnExternalChanges="true" /> 

    </configSections>

    <CustomSection fileName="default.txt" maxUsers="1000" maxIdleTime="00:15:00" />

 

</configuration>

就改这个就行了。

FileName="default.txt"

fileName="default.txt"

支持(0) 反对(0) Launcher | 园豆:45045 (高人七级) | 2015-03-18 16:22

@Launcher: 还是不行,我把<CustomerSection放在configuration结点下没问题吧?

支持(0) 反对(0) 北在北方 | 园豆:180 (初学一级) | 2015-03-18 16:28

@北在北方: 放在哪儿,看你自己给出的链接:https://msdn.microsoft.com/zh-cn/library/system.configuration.configurationsection.aspx

支持(0) 反对(0) Launcher | 园豆:45045 (高人七级) | 2015-03-18 16:32

@北在北方: 自己给的链接里就是小写的。。。。

支持(0) 反对(0) 球球_2014 | 园豆:200 (初学一级) | 2017-06-13 10:59
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册