Asp.net Web Api模型从发布时返回null

Asp.net Web Api模型从发布时返回null,asp.net,angular,asp.net-web-api,json.net,webapi,Asp.net,Angular,Asp.net Web Api,Json.net,Webapi,1) Web Api控制器 [Route("InsertRecipes")] public HttpResponseMessage PostRecipes(Recipes model) { db.Recipes.Add(recipes); db.SaveChanges(); var message = Request.CreateResponse(HttpStatusCode.Crea

1) Web Api控制器

    [Route("InsertRecipes")]
        public HttpResponseMessage PostRecipes(Recipes model)
        {
            db.Recipes.Add(recipes);
            db.SaveChanges();
            var message = Request.CreateResponse(HttpStatusCode.Created, model);
            return message;
return CreatedAtRoute("DefaultApi", new { id = recipes.ID }, recipes);
        }
2) Recipes.cs(模型类
在此处输入代码

[Table("tbl_Recipes")]
public class Recipes
{
    public string Name { get; set; }
    public string Description { get; set; }
    public string ImagePath { get; set; }
    public Ingredients Ingredients;

}
3) 在调用POST方法时

storeRecipes(){
        const recipes = this.recipeService.getRecipes();
        this.http.post('http://localhost:62286/Api/Recipes/InsertRecipes',recipes).pipe()
                .subscribe(response =>{
                    console.log(response);
                });
    }
4) 这是从Angular接收的JSON

 {
    "name": "Fish Curry",
    "description": "Fish Curry - taste really awesome",
    "imagePath": "https://ichef.bbci.co.uk/food/ic/food_16x9_1600/recipes/fish_curry_09718_16x9.jpg",
    "ingredients": [
      {
        "name": "Green Chilli",
        "amount": 5
      },
      {
        "name": "Fish",
        "amount": 1
      },
      {
        "name": "Ginger Galic Paste",
        "amount": 1
      },
      {
        "name": "Onion",
        "amount": 2
      },
      {
        "name": "Tomato",
        "amount": 2
      },
      {
        "name": "Master",
        "amount": 3
      },
      {
        "name": "Masala",
        "amount": 2
      }
    ]
  }

从angular发帖时,我始终在该控制器的模型中接收空值。

您应该使用
FromBody
[HttpPost]
。您将模型(这里是配方)放在http请求的主体中,这就是您应该从主体接收它的原因

    [Route("InsertRecipes")]
        public HttpResponseMessage PostRecipes(Recipes model)
        {
            db.Recipes.Add(recipes);
            db.SaveChanges();
            var message = Request.CreateResponse(HttpStatusCode.Created, model);
            return message;
return CreatedAtRoute("DefaultApi", new { id = recipes.ID }, recipes);
        }
在web api中修改
PostRecipes
,如下所示:

  [Route("InsertRecipes")]
  [HttpPost]
        public HttpResponseMessage PostRecipes([FromBody]Recipes model)
        {
            db.Recipes.Add(recipes);
            db.SaveChanges();
            var message = Request.CreateResponse(HttpStatusCode.Created, model);
            return message;
return CreatedAtRoute("DefaultApi", new { id = recipes.ID }, recipes);
        }
然后在你的服务中:

public storeRecipes() {
    // make sure that recipes is not null ....
    const recipes = this.recipeService.getRecipes();
    this.http.post('http://localhost:62286/Api/Recipes/InsertRecipes', recipes, this.setHttpHeader()).pipe()
        .subscribe(response => {
            console.log(response);
        });
}
private setHttpHeader() {
    const headers = new HttpHeaders().set('Accept', 'application/json').set('Content-Type', 'application/json');
    let options = { headers: headers };
    return options;
}

您错过了
[HttpPost]
[FromBody]
,请按如下操作:

[HttpPost]
[Route("InsertRecipes")]
        public HttpResponseMessage PostRecipes([FromBody] Recipes model)
        {
            db.Recipes.Add(recipes);
            db.SaveChanges();
            var message = Request.CreateResponse(HttpStatusCode.Created, model);
            return message;
return CreatedAtRoute("DefaultApi", new { id = recipes.ID }, recipes);
        }

你到底在哪里看到空的?getRecipes()正在返回上面4)中列出的JSON?您是否在console.log(response)中看到null?在web Api模型“public HttpResponseMessage PostRecipes(Recipes模型)”中没有,然后我在模型中也看到null。请查找用于从不同服务获取配方的代码getRecipes(){返回this.recipes.slice();}是否确实在
const recipes=this.recipesService.getRecipes()中有数据私人菜谱:菜谱[]=[新菜谱('鱼咖喱','鱼咖喱-味道真棒','',[新配料('绿辣椒',5),新配料('鱼',1),新配料('姜汁酱',1),新配料('Onion',2),新配料('Tomato',2),新配料('Master',3),…]);我通过getRecipes()收到这些配料{返回this.recipes.slice();}