0

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(模型类enter code here

[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) 在 Angular 中调用 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 发布时,我一直在该控制器的模型中收到 null 。

4

2 回答 2

0

您应该使用FromBody[HttpPost]。您将模型(这里是食谱)放在您的 http 请求的正文中,这就是您应该从 body 接收它的原因。

修改您的PostRecipesin web api,如下所示:

  [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;
}
于 2019-12-03T22:09:06.990 回答
0

您错过了[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);
        }
于 2019-12-05T10:43:43.767 回答