Angular 2& .NET Core Web Api HttpPost问题

时间:2017-02-25 07:52:37

标签: angular asp.net-core asp.net-core-webapi

我在同一个主题上看过一两篇帖子,但这两个问题的解决方案都不适用于我,所以我会在这里问一下。

我有一个角度为2(2.4)的应用程序正在调用.netCore(1.0)Web API。

我对网络API的请求工作正常。我的网络API允许它上面的所有方法(它确实限制了原点,但因为获取有效,我认为这不是问题)。

我的请求到达API,但请求内容未反序列化为参数对象。 webapi上的参数始终为null。

目前我的web api方法是(用于测试目的):

 [HttpPost]        
    public Member Post([FromBody] DocMe.Model.Provider.Member member)
    {
        return member;
    }

我的请求如下:

enter image description here

我从Angular生成请求的代码是:

public post(url: string, data: any): Observable<Response> {       
    this.headers = new Headers();
    this.headers.append('Content-Type', 'application/json');
    this.headers.append('Accept', 'application/json');

    let token = this.authService.GetToken();

    if (token !== '') {
        this.headers.append('Authorization', 'Bearer ' + token);
    }     
    return this.http.post(url, JSON.stringify(data), { headers: this.headers });
}

devtools图像中的Request Payload准确地表示传递给方法的数据对象的内容。

当我调试方法时,我看到呼叫通过签名但是使用&#34;成员&#34; parameter = null。请求有效负载与成员类型的属性匹配(此处的例外是有效负载是camelCase,而成员类型是PascalCase)。

我的POCO定义是:

public class Member
{
    public Member()
    {
        this.Id = Guid.NewGuid();
        this.MemberTypeId = 1;
        this.Deleted = false;

        this.MemberPractices = new HashSet<MemberPractice>();
        this.Educations = new HashSet<Education>();
        this.Insurances = new HashSet<Insurance>();
        this.Languages = new HashSet<Language>();
        this.Specialties = new HashSet<Specialty>();
        this.WorkHours = new HashSet<WorkHour>();
        this.WorkHoursOverrides = new HashSet<WorkHoursOverride>();
    }

    [Key]        
    [Display(Name = "Id")]
    public Guid Id { get; set; } // uniqueidentifier, not null        
    [Display(Name = "Member Type Id")]
    public int MemberTypeId { get; set; } // int, not null
    [MaxLength(50)]
    [StringLength(50)]
    [Display(Name = "NPI")]
    public string NPI { get; set; } // varchar(50), null
    [MaxLength(100)]
    [StringLength(100)]
    [Display(Name = "First Name")]
    public string FirstName { get; set; } // nvarchar(100), null
    [MaxLength(100)]
    [StringLength(100)]
    [Display(Name = "Last Name")]
    public string LastName { get; set; } // nvarchar(100), null
    [MaxLength(256)]
    [StringLength(256)]
    [Display(Name = "Contact Email")]
    public string ContactEmail { get; set; } // nvarchar(256), null
    [MaxLength(10)]
    [StringLength(10)]
    [Display(Name = "Gender")]
    public string Gender { get; set; } // varchar(10), null
    [MaxLength]
    [Display(Name = "Biography")]
    public string Biography { get; set; } // varchar(max), null
    [MaxLength(450)]
    [StringLength(450)]
    [Display(Name = "Identity Id")]
    public string IdentityId { get; set; } // nvarchar(450), null        
    [Display(Name = "Deleted")]
    public bool Deleted { get; set; } // bit, not null
    [Display(Name = "Deleted Date")]
    public DateTime? DeletedDate { get; set; } // datetime, null
    [Display(Name = "Deleted By")]
    public Guid? DeletedBy { get; set; } // uniqueidentifier, null

    [ForeignKey("Id")]
    public LkupMemberType LkupMemberType { get; set; }
    public ICollection<MemberPractice> MemberPractices { get; set; }
    public ICollection<Education> Educations { get; set; }
    public ICollection<Insurance> Insurances { get; set; }
    public ICollection<Language> Languages { get; set; }
    public ICollection<Specialty> Specialties { get; set; }
    public ICollection<WorkHour> WorkHours { get; set; }
    public ICollection<WorkHoursOverride> WorkHoursOverrides { get; set; }
}

负责填写请求有效负载的我的前端模型是:

export class ProviderModel {
public id: string;
public npi: string;
public firstName: string;
public lastName: string;
public contactEmail: string;
public gender: string;
public biography: string;
public specialties: SpecialityModel[];
public educations: EducationModel[];
public insurances: InsuranceModel[];
public languages: LanguageModel[];

constructor() {
    this.id = null;
    this.specialties =  [];
    this.educations = [];
    this.insurances = [];
    this.languages = [];
}
}

考虑到新的.net核心是什么,我不确定我是做错了什么,或者是否有某种我可能偶然发现的错误。

我已经盯着这个问题几天了,所以我想把它放在那里测试我的理智,并希望它已经解决,所以我可以从一些应该相对微不足道的东西继续前进。

1 个答案:

答案 0 :(得分:1)

更新:我怀疑HashSet是问题所在。考虑为HashSet编写自己的JsonConverter。

==

您是否为Web API配置了SerializerSettings来解析Camel大小写?

 services.AddMvc().AddJsonOptions(options =>
                  {
                      options.SerializerSettings.ContractResolver =
                          new CamelCasePropertyNamesContractResolver();
                  });