将FormData从Angular发布到Web API,数据始终为null

时间:2018-04-11 14:28:20

标签: c# angular post asp.net-web-api2

我将一些formData从我的Angular 5应用程序发布到我的Web API。这是打字稿代码:

public postFormData() {
    const form = $('#customListForm')[0];
    const formData = new FormData();

    formData.append('tenant', form[0].value);
    formData.append('username', form[1].value);
    formData.append('password', form[2].value);

    formData.append('CL_title', form[3].value);
    formData.append('CL_description', form[4].value);
    formData.append('CL_contentType', form[5].value);
    formData.append('CL_template', form[6].value);

    // const json = JSON.stringify(formData);

    this.http.post(this.apiURL, formData, this.httpOptions).catch(this.errorHandler).subscribe(res => this.formPosted(res));
    // this.http.get(this.apiURL).catch(this.errorHandler).subscribe(res => this.formPosted(res));
}  

这是我的httpOptions:

private httpOptions = {
    headers: new HttpHeaders({
      'Content-Type': 'application/x-www-form-urlencoded; charset=utf-8'
    })
  };  

我尝试了各种内容类型,例如application / json,或multipart / form-data,但也没有用。

这是我的Controller Post Action:

// POST api/customlist
    [HttpPost]
    public System.Web.Http.IHttpActionResult Post(CustomList data)
    {
        var test = HttpContext.Current.Request;
        var testje = data;

        return Ok("POST all good");
    }  

在这里,您可以看到我的CustomList自定义对象:

public class CustomList
{
    public CustomList()
    {

    }

    public string Tenant { get; set; }
    public string Username { get; set; }
    public string Password { get; set; }
    public string CL_title { get; set; }
    public string CL_description { get; set; }
    public string CL_contentType { get; set; }
    public string CL_template { get; set; }
}  

正如您所看到的,它包含与我的formData&相同的字段。有一个默认/空构造函数。

无论我尝试什么,我总是将我的“数据”变为空...任何想法?

0 个答案:

没有答案