Asp.Net Core Web API修补程序Id始终为NULL



我有一个asp.net核心web api,它的端点使用PATCH。需要更新记录的单个字段。我已经设置了端点来接受Json补丁文档主体中的记录的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参数显示为null。

报表ID参数显示空

API调用:http://localhost:4200/api/updateDesc/A88FCD08-38A3-48BB-8E64-61057B3C0B1F我使用的是poster,API端点正在正确发送和读取JSON主体。车身返回

导致路由中的ID未分配给reportID参数的原因是什么?

我也尝试了一个稍微不同的终点,结果相同

[HttpPatch("{id}")]
public IActionResult updateDescription(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!");
}

请立即尝试

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

最新更新