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 。