MVC4中的自定义配置部分为空值

时间:2013-08-08 15:39:52

标签: asp.net asp.net-mvc-4 custom-configuration

我有这个课程

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为全零且公司ID为零

这是MVC web api项目中app_code文件夹中的一个类。!!

1 个答案:

答案 0 :(得分:0)

编辑属性主体以从父成员获取数据

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