Angular 2 & .NET Core Web Api HttpPost 问题
Angular 2 & .NET Core Web Api HttpPost issue
我看过一两篇关于同一主题的帖子,但没有一个解决这些问题的解决方案对我有用,所以我要在这里问一下。
我有一个 angular 2 (2.4) 应用程序正在调用 .netCore (1.0) Web API。
我对网络的获取请求 api 工作正常。我的网络 api 允许其上的所有方法(它确实限制来源,但因为 get 有效,我的印象是这不是问题)。
我的请求到达了API,但是请求内容没有反序列化到参数对象中。网络上的参数api 始终为空。
目前我的网络 api 方法是(用于测试目的):
[HttpPost]
public Member Post([FromBody] DocMe.Model.Provider.Member member)
{
return member;
}
我的请求是这样的:
我从 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 准确地表示了传递给方法的数据对象的内容。
当我调试该方法时,我看到调用通过了签名,但 "member" 参数 = null。请求负载匹配成员类型的属性(这里的例外是负载是驼峰式,而成员类型是 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; }
}
我负责填充 Request Payload 的前端模型是:
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 的问题。考虑为 HashSet 编写自己的 JsonConverter。
======
您是否为您的 Web API 配置了 SerializerSettings 来解析 Camel 大小写?
services.AddMvc().AddJsonOptions(options =>
{
options.SerializerSettings.ContractResolver =
new CamelCasePropertyNamesContractResolver();
});
我看过一两篇关于同一主题的帖子,但没有一个解决这些问题的解决方案对我有用,所以我要在这里问一下。
我有一个 angular 2 (2.4) 应用程序正在调用 .netCore (1.0) Web API。
我对网络的获取请求 api 工作正常。我的网络 api 允许其上的所有方法(它确实限制来源,但因为 get 有效,我的印象是这不是问题)。
我的请求到达了API,但是请求内容没有反序列化到参数对象中。网络上的参数api 始终为空。
目前我的网络 api 方法是(用于测试目的):
[HttpPost]
public Member Post([FromBody] DocMe.Model.Provider.Member member)
{
return member;
}
我的请求是这样的:
我从 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 准确地表示了传递给方法的数据对象的内容。
当我调试该方法时,我看到调用通过了签名,但 "member" 参数 = null。请求负载匹配成员类型的属性(这里的例外是负载是驼峰式,而成员类型是 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; }
}
我负责填充 Request Payload 的前端模型是:
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 的问题。考虑为 HashSet 编写自己的 JsonConverter。
======
您是否为您的 Web API 配置了 SerializerSettings 来解析 Camel 大小写?
services.AddMvc().AddJsonOptions(options =>
{
options.SerializerSettings.ContractResolver =
new CamelCasePropertyNamesContractResolver();
});