我有这门课
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 文件夹中的一个类。!!