asp.netasp.net-mvc-4custom-configuration

Custom Config Section in MVC4 empty values


i have this class

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;
        }
    }
}

and this 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>

my method on the HeaderSampleData Class returns an object with all values empty strings are null guid is all zeros and company id zero

this is a class in an app_code folder in an MVC web api project.!!


Solution

  • edited the properties bodies to get data from parent members

        [ConfigurationProperty("CompanyId",  IsRequired = true)]
        public int CompanyId
        {
            get { return (int)this["CompanyId"]; }
            set { this["CompanyId"] = value; }
        }