【问题标题】:Angular 6 Post body with array inside request object to web APIAngular 6 Post body 与请求对象内的数组到 Web API
【发布时间】:2019-01-13 04:36:05
【问题描述】:

我想将数据从 angular post 传递到 web api。但在 web api 中它显示为 null。

我的意图是通过 POST 请求将对象内部的列表传递给 Web Api。

Angular 服务 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 属性。

我用谷歌的 POST MAN 用原始的 Body json 进行了测试。

{"sortcolumn":"CustomerName", "sortorder":"asc", "pagenumber":"0", "pagesize": "10","ddlUnits":[{"Value": "20022", “文本”:“Arvind”},{“值”:“20022”,“文本”:“Arvind”}]}

效果很好

见Inspact element Network error screen shot

【问题讨论】:

  • 您能提供更多信息吗?您尝试了什么,什么不起作用...
  • 请检查问题现在我已经提到了所有细节。
  • 您是否碰巧解决了这个问题?我在 Angular 6 上遇到了类似的问题

标签: angular


【解决方案1】:

由于属性名称的大小写,您很可能遇到问题。默认情况下,从 JSON 转换为 c# 时,小写属性名称会更改为大写首字母,因此如果您将 sortorder、sortcolumn、ddlUnits 等更改为具有大写首字母,它可能会起作用(前提是您将数据作为 application/json 内容传递类型)

【讨论】:

  • 是的,但它使用 POST MAN 发布然后工作并返回响应。但是从角度无法击中那个动作,我已经在 web api 的动作中设置了断点,但没有调用
  • 请注意,您的 Angular JSON 属性名称周围没有引号,而邮递员却有
猜你喜欢
  • 2017-12-09
  • 2018-12-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-04-19
  • 2019-08-02
  • 2018-09-03
相关资源
最近更新 更多