Пользовательский раздел конфигурации в MVC4 пустые значения

У меня есть этот класс

public class HeaderSampleData : ConfigurationSection  
{
    [ConfigurationProperty("CompanyId",  IsRequired = true)]
    public int CompanyId { get; set; }

    [ConfigurationProperty("ApiKey", IsRequired = true)]
    public Guid ApiKey { get; set; }

    [ConfigurationProperty("UserName", IsRequired = true)]
    public string UserName { get; set; }

    [ConfigurationProperty("Password", IsRequired = true)]
    public string Password { get; set; }

    private static HeaderSampleData _instance;

    public static HeaderSampleData Instance {
        get {
            if (_instance == null) {
                _instance = ConfigurationManager.GetSection("HeaderSampleZ") as HeaderSampleData;
            }

           return  _instance;
        }
    }
}

и этот web.config

<configSections>
<!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
<section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=4.4.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false"/>
<section name="HeaderSampleZ" type="MYApp.App_Code.HeaderSampleData" allowLocation="true" allowDefinition="Everywhere"/>
</configSections>
  <HeaderSampleZ UserName="email@domain.com" Password="somePassword" CompanyId="1" ApiKey="aZaf2bZ2-a517-4b99-aaZb-2e9e4b187Zc7"></HeaderSampleZ>

мой метод в классе HeaderSampleData возвращает объект со всеми значениями пустые строки равны нулю guid все нули и идентификатор компании ноль

это класс в папке app_code в проекте веб-API MVC.!!

1 ответ

Решение

Отредактировал тела свойств для получения данных от родительских элементов

    [ConfigurationProperty("CompanyId",  IsRequired = true)]
    public int CompanyId
    {
        get { return (int)this["CompanyId"]; }
        set { this["CompanyId"] = value; }
    }
Другие вопросы по тегам