ConfigurationManager.GetSection返回null以显示正确的'path'

时间:2009-12-22 12:03:24

标签: asp.net web-config custom-configuration

这是关于web.config文件

这是ConfigSection

<configSections>
<sectionGroup name="HttpExceptionHandler">
  <section name="errorLog" type="System.Configuration.SingleTagSectionHandler, System, Version=1.0.5000.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
  <section name="errorMail" type="System.Configuration.SingleTagSectionHandler, System, Version=1.0.5000.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
</sectionGroup>

这是SectionGroup:

<HttpExceptionHandler>
    <errorLog type="MI.Generic.HttpExceptionHandler.SqlErrorLog, MI.Generic.HttpExceptionHandler" dataSource="opentraderdev\dev" initialCatalog="MiTraderError" />
</HttpExceptionHandler>

以下是代码:

public class ErrorLogConfiguration : ConfigurationSection
{
    public static ErrorLogConfiguration GetConfig()
    {
        return ConfigurationManager.GetSection("HttpExceptionHandler\\errorLog") as ErrorLogConfiguration;
    }

    [ConfigurationProperty("initialCatalog", IsRequired = true)]
    public string InitialCatalog
    {
        get
        {
            return this["initialCatalog"] as string;
        }
    }

    [ConfigurationProperty("dataSource", IsRequired = true)]
    public string DataSource
    {
        get
        {
            return this["dataSource"] as string;
        }
    }
}

返回始终为null。我已经没想完了。任何帮助表示赞赏。

1 个答案:

答案 0 :(得分:8)

如何切换斜线方向:

return ConfigurationManager.GetSection("HttpExceptionHandler/errorLog") as ErrorLogConfiguration;

这是similar example from MSDN