2017-02-25 182 views
0

我在同一主題上看到過一兩個帖子,但這兩個問題的解決方案都不適合我,所以我會在這裏問一下。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; 
    } 

我的要求是這樣的:

enter image description here

我的代碼生成請求從角是:

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核心有多新,我不知道如果我做錯了什麼,或者有什麼我可能遇到的錯誤。

我一直在盯着這個問題幾天,所以我想把它放在那裏來測試我的理智,希望它解決了,所以我可以繼續從應該是相對微不足道的東西。

+0

代碼在哪裏? – Aravind

+0

我的第一個嫌疑人是JSON序列化設置... – Adil

+0

你也可以粘貼你的POCO(會員級別) – Adil

回答

1

更新:我懷疑HashSet是問題。考慮編寫你自己的JsonConverter for HashSet。

======

你有沒有配置你的Web API的SerializerSettings解決駱駝外殼?

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

我原來分配給DefaultContractResolver()。我將它改爲CamelCasePropertyNamesContractResolver,只是爲了看看這是否會起作用,而我也有同樣的問題。 – JakeHova

+0

是否有更好的方式來處理進入的對象列表,以便我不必爲它們編寫自定義轉換器?可以使用List類型而不是HashSet更好地工作嗎? – JakeHova

+0

我的期望是列表應該在框外運行......但那麼你將不具備HashSet功能。如果List對你來說足夠好,那麼可以考慮擺脫HashSet。這篇文章可能會幫助你:http://stackoverflow.com/questions/28672082/asp-net-entity-framework-6-hashset-or-list-for-a-collection – Adil