private static IActionResult DoPost(HttpRequest req) { string json = new StreamReader(req.Body).ReadToEnd(); BookChapter chapter = JsonConvert.DeserializeObject <BookChapter>(json); s_bookChaptersService.Add(chapter); return(new OkResult()); }
public IActionResult PostBookChapter([FromBody] BookChapter chapter) { if (chapter == null) { return(BadRequest()); } _bookChaptersService.Add(chapter); return(CreatedAtRoute(nameof(GetBookChapterById), new { id = chapter.Id }, chapter)); }
public IActionResult PostBookChapter([FromBody] BookChapter chapter) { //如果参数BookChapter为空,则返回BadRequest(HTTP错误400) if (chapter == null) { return(BadRequest()); } _bookChaptersService.Add(chapter); // CreatedAtRoute返回HTTP状态201(已创建),并且对象已序列化。 // 返回的头信息包含指向资源的链接,即指向GetBookChapterById的链接,其id设置为新创建的对象的标识符 return(CreatedAtRoute(nameof(GetBookChapterById), new { id = chapter.Id }, chapter)); }