我想将数据从角度发布传递到Web API。但是在网络API中,它显示为空。
我的意图是通过POST请求将对象内部的列表传递给Web Api。
Angular Service POST方法:
postData(URL : string,body : any) : Observable<any> {
var dto = {sortcolumn:"CustomerName", sortorder:"asc", pagenumber:"0",pagesize: "10"
,ddlUnits: [{"Value": "20022", "Text": "Arvind"},{"Value": "20022", "Text": "Arvind"}]
};
let bodystr = JSON.stringify(dto);
const httpOptions = {
headers: new HttpHeaders({
//'Content-Type': 'application/x-www-form-urlencoded',
'Content-Type': 'application/json',
})
};
return this._http.post<any>(this.BaseURL + URL, bodystr,httpOptions)
//当我调用此服务方法时,将在组件中进行订阅。
}
WEB API操作方法:
[Route("api/AllCustomers")]
[HttpPost]
public IHttpActionResult GetAllCustomerPOST([FromBody] CustomerDTO dto)
{
try
{
List<GetAllCustomer_Result> lst = clsDCustomer.GetAllCustomer().ToList();
return new CustomResponseResult(HttpStatusCode.OK, Request, lst);
}
catch
{
return new CustomResponseResult(HttpStatusCode.InternalServerError, Request, Constants.MSGInternamServerError);
}
}
CLASS结构(在操作请求中传递)
public class CustomerDTO
{
public long ID { get; set; }
[Required(ErrorMessage = "Required")]
[Display(Name = "Customer Name")]
public string CustomerName { get; set; }
[Required(ErrorMessage = "Required")]
[MaxLength(length: 10, ErrorMessage = "Maximun length for mobile no is 10 digit")]
[Display(Name = "Mobile No")]
public string MobileNo { get; set; }
[Display(Name = "Contact No")]
public string ContactNo { get; set; }
[Display(Name = "Address")]
public string Address { get; set; }
public int CreatedBy { get; set; }
public System.DateTime CreatedOn { get; set; }
public List<DropDownList> ddlUnits { get; set; }
public int pagenumber { get; set; }
public int pagesize { get; set; }
public string sortcolumn { get; set; }
public string sortorder { get; set; }
}
public class DropDownList
{
public string Value { get; set; }
public string Text { get; set; }
}
我已经在HttpParams()中使用“ Content-Type”:'application / x-www-form-urlencoded'标头传递了主体数据,并且可以在WEB API中获取ddlUnits属性。
我使用原始的Body json与Google的POST MAN进行了测试。
{“ sortcolumn”:“ CustomerName”,“ sortorder”:“ asc”,“ pagenumber”:“ 0”,“ pagesize”:“ 10”,“ ddlUnits”:[{“ Value”:“ 20022”, “ Text”:“ Arvind”},{“ Value”:“ 20022”,“ Text”:“ Arvind”}]}
效果很好
答案 0 :(得分:0)
由于属性名称的大小写,您很有可能遇到问题。默认情况下,从JSON转换为c#时,小写属性名称会更改为大写首字母,因此,如果将sortorder,sortcolumn,ddlUnits等更改为大写首字母,则可能会起作用(前提是您将数据作为application / json内容传递类型)