求助,在实现自定义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;
}
}
}
}
FileName="default.txt"
fileName="default.txt"
不起作用
<CustomerSection fileName="default.txt" maxUsers="1000" maxIdleTime="00:15:00" />
@北在北方:
public string fileName
{
get {
return (string)this["fileName"];
@Launcher: 你在蒙?如果这里需要改成小写,那我两处都是大写跟都是小写应该都一样
这是官网的代码示例:https://msdn.microsoft.com/zh-cn/library/system.configuration.configurationsection.aspx
我是照着官网示例敲的代码
@北在北方:
<?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"
@Launcher: 还是不行,我把<CustomerSection放在configuration结点下没问题吧?
@北在北方: 放在哪儿,看你自己给出的链接:https://msdn.microsoft.com/zh-cn/library/system.configuration.configurationsection.aspx
@北在北方: 自己给的链接里就是小写的。。。。