Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-core/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/http/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net core WebAPI POST-邮递员中出现500错误-如何解决?_Asp.net Core_Http Post_Postman_Asp.net Core Webapi - Fatal编程技术网

Asp.net core WebAPI POST-邮递员中出现500错误-如何解决?

Asp.net core WebAPI POST-邮递员中出现500错误-如何解决?,asp.net-core,http-post,postman,asp.net-core-webapi,Asp.net Core,Http Post,Postman,Asp.net Core Webapi,我正在尝试使用postman来测试我的POST方法,但是我得到了一个500错误,当在我的api的try-catch方法中调用它时,它会失败并抛出错误,但我不确定为什么或者如何找到 我想在我的“消息链”中输入任何虚拟数据,我已经复制了下面的代码,有人能告诉我我做错了什么吗?我对使用webapi和postman非常陌生,所以我自己也不知道如何跟踪哪里出了问题 编辑-我如何显示邮递员中的500错误的更多细节,以便我可以看到它被抛出的原因 // POST: api/Message_Chain

我正在尝试使用postman来测试我的POST方法,但是我得到了一个500错误,当在我的api的try-catch方法中调用它时,它会失败并抛出错误,但我不确定为什么或者如何找到

我想在我的“消息链”中输入任何虚拟数据,我已经复制了下面的代码,有人能告诉我我做错了什么吗?我对使用webapi和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:api/Message\u链
//若要防止套印攻击,请启用要绑定到的特定属性,例如
//更多详细信息,请参阅https://go.microsoft.com/fwlink/?linkid=2123754.
[HttpPost]
公共异步任务后消息链(消息链消息链)
{
_context.Message\u Chain.Add(Message\u Chain);
尝试
{
wait_context.SaveChangesAsync();
}
捕获(DbUpdateException)
{
if(Message_ChainExists(Message_Chain.MessageChainId))
{
返回冲突();
}
其他的
{
投掷;
}
}
返回CreateDataAction(“GetMessage_Chain”,新的{id=message_Chain.MessageChainId},message_Chain);
}
公共类消息链
{
[关键]
public int MessageChainId{get;set;}
public int MessageSubjectId{get;set;}
公共字符串MessageBody{get;set;}
public int SentFromId{get;set;}
公共日期时间日期时间{get;set;}
公共消息主题消息主题{get;set;}
公共ICollection同事_消息{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"
}
POST>http:///api/Message_Chain
BODY>JSON
{
“messageChainId”:10,
“消息主体”:10,
“messageBody”:“测试我的post到postman的消息链方法”,
“sentFromId”:1000001,
“日期时间”:“2020-11-05T12:15:00”
}
试试这个

[HttpPost]
    public async Task<ActionResult<Message_Chain>> PostMessage_Chain([FromBody] 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);
    }
[HttpPost]
公共异步任务后消息链([FromBody]消息链消息链)
{
_context.Message\u Chain.Add(Message\u Chain);
尝试
{
wait_context.SaveChangesAsync();
}
捕获(DbUpdateException)
{
if(Message_ChainExists(Message_Chain.MessageChainId))
{
返回冲突();
}
其他的
{
投掷;
}
}
返回CreateDataAction(“GetMessage_Chain”,新的{id=message_Chain.MessageChainId},message_Chain);
}

谢谢您的回复,运气不好,不幸的是我收到了相同的错误。请在控制台中检查错误类型。因为我无法在这段代码中重现错误。