自定义配置部分无效

时间:2017-04-15 19:46:02

标签: c# .net asp.net-mvc-5 web-config config

我不太确定我做错了什么。我一直在这里关注这篇文章:https://msdn.microsoft.com/en-us/library/2tw134k3.aspx并且根据我的理解,我正确地进行了此设置。这是我的代码:

public class LoggerConfiguration : ConfigurationSection
{
    [ConfigurationProperty("level")]
    public LoggerConfigurationLevel Level
    {
        get { return (LoggerConfigurationLevel) this["level"]; }
        set { this["level"] = value; }
    }
}

public class LoggerConfigurationLevel : ConfigurationElement
{
    [ConfigurationProperty("logLevel", IsRequired = true)]
    [StringValidator(MaxLength = 5, MinLength = 4, InvalidCharacters = "!@#$%^&*()_+12345567890-=`~[]\\;',./{}|:\"<>?HJKMPQSVXYZ")]
    public string LogLevel
    {
        get { return (string)this["logLevel"]; }
        set { this["logLevel"] = value; }
    }

    [ConfigurationProperty("fileExtension", IsRequired = true)]
    [StringValidator(InvalidCharacters = "!@#$%^&*()_+12345567890-=`~[]\\;',./{}|:\"<>?")]
    public string FileExtension
    {
        get { return (string)this["fileExtension"]; }
        set { this["fileExtension"] = value; }
    }

    [ConfigurationProperty("logDirectory", DefaultValue = "", IsRequired = false)]
    public string LogDirectory
    {
        get { return (string)this["logDirectory"]; }
        set { this["logDirectory"] = value; }
    }

    [ConfigurationProperty("messageLayout", DefaultValue = "${message}${exception:format=tostring}", IsRequired = false)]
    public string MessageLayout
    {
        get { return (string)this["messageLayout"]; }
        set { this["messageLayout"] = value; }
    }
}

然后我的web.config看起来像这样:

<configuration>
  <configSections>
    <sectionGroup name="loggerConfigurationGroup">
        <section  name="logggerConfiguration" 
                  type="Website.Models.LoggerConfiguration"  
                  allowLocation="true" 
                  allowDefinition="Everywhere" />
    </sectionGroup>
  </configSections>
  <loggerConfigurationGroup>
    <loggerConfiguration>
      <loggerConfigurationLevel>
        <level LogLevel="Debug" FileExtension="log" LogDirectory="C:\Logs" MessageLayout="${message}${exception:format=tostring}" />
      </loggerConfigurationLevel>
    </loggerConfiguration>
  </loggerConfigurationGroup>
</configuration>

我得到的错误是我的网站加载了一个空白页面并在控制台中(在浏览器上的开发人员工具中)它显示500错误。我确信这是因为我在web.config中配置错误,我无法弄清楚是什么。

编辑:

所以我决定回到第1步并尝试实现上面给出的链接中显示的示例。有趣的是,这对我来说也不起作用。

我复制了代码和网络配置,当我打印出字体名称中的值时,我得到了Aria,而不是web.config中列出的TimesNewRoman。

那篇文章过时了吗?还有其他方法可以实现这个目标吗?

EDIT2:

所以我简化了这段代码,这是我现在的代码:

public sealed class MailCenterConfiguration : ConfigurationSection
{
    [ConfigurationProperty("userDiskSpace", IsRequired = true)]
    [IntegerValidator(MinValue = 0, MaxValue = 1000000)]
    public int UserDiskSpace
    {
        get { return (int)base["userDiskSpace"]; }
        set { base["userDiskSpace"] = value; }
    }
}

我的Web.config:

<configuration>
    <configSections>
        <section name="mailCenter" type="Website.Models.MailCenterConfiguration" requirePermission="false"/>
    </configSections>
    <mailCenter userDiskSpace="25000"></mailCenter>
</configuration>

现在页面加载但是它吐出的值是0,而不是25000。

以上代码基于SO答案:How do I define custom web.config sections with potential child elements and attributes for the properties?

0 个答案:

没有答案