我正在尝试编写这个接受 POST 请求的控制器。
我需要这个控制器来添加一本新书,并将新书 bookId 添加到另一个名为 StoreList 的对象中。
所以我试图传入新的 bookList,以及需要将 bookId 添加到其中的 storeList。
// POST: api/BookList
[HttpPost]
public async Task<ActionResult<BookList>> PostBookList(BookList bookList, StoreList storeList)
{
_context.BookList.Add(bookList);
await _context.SaveChangesAsync();
_context.Entry(storeList).State = EntityState.Modified;
try
{
await _context.SaveChangesAsync();
}
catch (DbUpdateConcurrencyException)
{
if (!StoreListExists(storeId))
{
return NotFound();
}
else
{
throw;
}
}
return CreatedAtAction("GetBookList", new { id = bookList.BookId }, bookList);
}
这是我的 API 端点:
这些是我在请求的 BODY 中传递的两个对象(新的 bookList 和现有的 storeList):
{
"bookId": "bc381612-c63b-4438-b35b-161a3a568fc7",
"bookTitle": "Is this a test 2?"
},
{
"storeId": "0001f801-6909-4b6e-8652-e1b49745280f",
"bookId": "bc381612-c63b-4438-b35b-161a3a568fc7"
}
但是每当我尝试“命中”那个端点时,我都会收到这个错误:
System.InvalidOperationException HResult=0x80131509 Message=Action 'DocumentStorageAPI.Controllers.Book.BookListController.PostBookList (DocumentStorageAPI)' 有多个参数被指定或推断为从请求正文绑定。每个动作只能从正文绑定一个参数。检查以下参数,并使用“FromQueryAttribute”指定从查询绑定,“FromRouteAttribute”指定从路由绑定,“FromBodyAttribute”用于从正文绑定的参数:BookList bookList StoreList storeList
如何让我的控制器允许我添加新 bookList 并更新所需的 storeList?
谢谢!