Asp.Net核心Web API修补程序ID始终为NULL

问题描述

我有一个asp.net核心网络api,其端点使用PATCH。需要更新一条记录的单个字段。我具有端点设置,可以接受Json Patch Document正文中记录的ID。

这是当前设置我的端点的方式。我只有足够的代码来检查是否可以找到匹配的ID,但该ID不能按预期工作。

[HttpPatch("{id}")]
    public IActionResult updateDescription( [Fromroute] string reportId,[FromBody] JsonPatchDocument<Reports> patchEntity)
    {

        //reportId is null... not showing the id being passed in the api call
        if (patchEntity == null)
        {
            return Ok("body is null");
        }
     

        var entity = _context.Reports.SingleOrDefault(x => x.ReportId == reportId);

        if (entity == null)
        {
            return Ok(reportId + " Why is entity null!");
        }

        //patchEntity.ApplyTo(entity,ModelState);

        return Ok(entity + " found!");
    }

当我调用传递ID的api时,报告ID参数显示为空。

report ID parameter shows null

API调用:http:// localhost:4200 / api / updateDesc / A88FCD08-38A3-48BB-8E64-61057B3C0B1F 我正在使用邮递员,并且API端点正在正确发送和读取JSON正文。 body return

我缺少什么导致路由ID没有分配给reportID参数?

我也尝试了稍有不同的终点,结果相同

[HttpPatch("{id}")]
    public IActionResult updateDescription(string reportId,ModelState);

        return Ok(entity + " found!");
    }

解决方法

请立即尝试

[HttpPatch("{reportId}")]
    public IActionResult updateDescription( [FromRoute] string reportId,[FromBody] JsonPatchDocument<Reports> patchEntity)
    {