【发布时间】:2019-12-03 19:34:21
【问题描述】:
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。
【问题讨论】:
-
你在哪里看到空值? recipeService.getRecipes() 是否返回上面 4) 中列出的 JSON?您在 console.log(response) 中看到 null 了吗?
-
没有在 web Api 模型"public HttpResponseMessage PostRecipes(Recipes 模型)"
标签: asp.net angular asp.net-web-api json.net webapi