模型的Angular-Web api post方法为null

时间:2018-10-22 02:22:25

标签: angular asp.net-web-api cors angular6

在下面的代码中,始终get model为null,但对于[frombody] string参数却不存在 在WebApiConfig中设置cors

 var cors = new EnableCorsAttribute(origins:"*",methods: "*", headers: "*");
        config.EnableCors(cors);

//控制器

[Route("SaveorUpdateCategory")]
    [HttpPost]
    public IHttpActionResult SaveorUpdateCategory(ModelCategory model)
    {
        string res = objcate.SaveorUpdateCategory(model);
        return Ok(res);

    }

//角度服务

  SaveorUpdateCategory( model: any): Observable<any> {
     this.headers= new Headers({ 'Content-Type': 'application/json' })
    this.options = new RequestOptions({ headers: this.headers });
   let body = JSON.stringify(model);
   return this._http.post(this.BaseURL+"Category/SaveorUpdateCategory", model, this.options)
       .map((response: Response) => <any>response.json())
        .catch(this.handleError);

}

模型样本为{categry:'Test',Id:'1'} 当传递单个变量获取值时,但模型始终为空

将cors安装为Microsoft.AspNet.WebApi.Cors安装包

1 个答案:

答案 0 :(得分:1)

使用FromBody如下更改您的Web api方法,

public void Disconnect([FromBody] ModelCategory model) {  }