Asp.Net Core Web API Patch Id Is Always NULL

613 Views Asked by At

I have a asp.net core web api with an endpoint using PATCH. Need to update a single field of a record. I have the endpoint setup to accept the id of the record along a Json Patch Document body.

Here is how my endpoint is currently setup. I just have enough code to check that I can find a matching id which is not working as expected.

[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!");
    }

When I call the api passing the Id the report ID parameter shows null.

report ID parameter shows null

API call: http://localhost:4200/api/updateDesc/A88FCD08-38A3-48BB-8E64-61057B3C0B1F I am using postman and the JSON body is being sent and read correctly by the API endpoint. body return

What am I missing that is causing the ID from the route to not be assigned to the reportID parameter?

I also tried a slight different endpoint as well with the same results

[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!");
    }
1

There are 1 best solutions below

0
On BEST ANSWER

Please try now

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