Angular 2&.NET核心Web Api HttpPost问题
我在同一主题上看到过一两个帖子,但这两个问题的解决方案都不适合我,所以我会在这里问一下。Angular 2&.NET核心Web Api HttpPost问题
我有一个调用.netCore(1.0)Web API的角2(2.4)应用程序。
我的获取请求到web api工作正常。我的web api允许使用所有的方法(它确实限制了原点,但因为get可以工作,所以我觉得这不是问题)。
我的请求到达API,但请求内容未反序列化到参数对象中。 webapi上的参数始终为空。
目前我的web API方法是(用于测试目的):
[HttpPost]
public Member Post([FromBody] DocMe.Model.Provider.Member member)
{
return member;
}
我的要求是这样的:
我的代码生成请求从角是:
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 ima中的Request Payload ge准确地表示传入方法的数据对象的内容。
当我调试该方法时,我看到呼叫通过签名但通过“member”参数= 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核心有多新,我不知道如果我做错了什么,或者有什么我可能遇到的错误。
我一直在盯着这个问题几天,所以我想把它放在那里来测试我的理智,希望它解决了,所以我可以继续从应该是相对微不足道的东西。
更新:我怀疑HashSet是问题。考虑编写你自己的JsonConverter for HashSet。
======
你有没有配置你的Web API的SerializerSettings解决骆驼外壳?
services.AddMvc().AddJsonOptions(options =>
{
options.SerializerSettings.ContractResolver =
new CamelCasePropertyNamesContractResolver();
});
我原来分配给DefaultContractResolver()。我将它改为CamelCasePropertyNamesContractResolver,只是为了看看这是否会起作用,而我也有同样的问题。 – JakeHova
是否有更好的方式来处理进入的对象列表,以便我不必为它们编写自定义转换器?可以使用List类型而不是HashSet更好地工作吗? – JakeHova
我的期望是列表应该在框外运行......但那么你将不具备HashSet功能。如果List对你来说足够好,那么可以考虑摆脱HashSet。这篇文章可能会帮助你:http://stackoverflow.com/questions/28672082/asp-net-entity-framework-6-hashset-or-list-for-a-collection – Adil
代码在哪里? – Aravind
我的第一个嫌疑人是JSON序列化设置... – Adil
你也可以粘贴你的POCO(会员级别) – Adil