【发布时间】:2021-02-18 04:16:00
【问题描述】:
我正在尝试使用邮递员来测试我的 POST 方法,但是我收到 500 错误,当在我的 api 的 try catch 方法中调用它时,尝试失败并抛出错误,但我不确定为什么或如何找出。
我想在我的“message_Chain”中输入任何虚拟数据,并且我已经复制了下面的代码,谁能告诉我我做错了什么?我对使用 web apis 和 postman 很陌生,所以我不知道如何自己跟踪哪里出了问题。
编辑 - 我如何在邮递员中显示 500 错误的更多详细信息,以便我可以看到为什么会抛出它?
// POST: api/Message_Chain
// To protect from overposting attacks, enable the specific properties you want to bind to, for
// more details, see https://go.microsoft.com/fwlink/?linkid=2123754.
[HttpPost]
public async Task<ActionResult<Message_Chain>> PostMessage_Chain(Message_Chain message_Chain)
{
_context.Message_Chain.Add(message_Chain);
try
{
await _context.SaveChangesAsync();
}
catch (DbUpdateException)
{
if (Message_ChainExists(message_Chain.MessageChainId))
{
return Conflict();
}
else
{
throw;
}
}
return CreatedAtAction("GetMessage_Chain", new { id = message_Chain.MessageChainId }, message_Chain);
}
public class Message_Chain
{
[Key]
public int MessageChainId { get; set; }
public int MessageSubjectId { get; set; }
public string MessageBody { get; set; }
public int SentFromId { get; set; }
public DateTime DateTime { get; set; }
public Message_Subject Message_Subject { get; set; }
public ICollection<Colleague_Message> Colleague_Message { get; set; }
}
}
这是我在邮递员中放的,
POST > http://<my url>/api/Message_Chain
BODY > JSON
{
"messageChainId": 10,
"messageSubjectId": 10,
"messageBody": "Testing my post to message chain method from postman",
"sentFromId": 1000001,
"dateTime": "2020-11-05T12:15:00"
}
【问题讨论】:
-
请在控制台检查错误类型。因为我无法重现此代码中的错误。
标签: asp.net-core http-post postman asp.net-core-webapi