0

I want to change values in database using http patch method. But it returns erorr "The target location specified by path segment was not found"

[HttpPatch()]
        [Route("[action]/{age}")]
        public IActionResult PatchEmployee([FromRoute] int age, [FromBody] JsonPatchDocument employeeDocument)
        {
            UpdateEmployeePatchAsync(age, employeeDocument);
            
            return Ok("Saved");
        }

        private async void UpdateEmployeePatchAsync(int age, JsonPatchDocument employeeDocument)
        {
            RepositoryContextFactory factory = new RepositoryContextFactory();
            RepositoryContext context = factory.CreateDbContext(null);
            List<Employee> employees = context.Employees.ToList();
            Employee employee = employees.Where(p => p.Age == age).FirstOrDefault();

            employeeDocument.ApplyTo(employee);
            await context.SaveChangesAsync();
        }

Get method returns the following result set:

[
    {
        "id": "80abbca8-664d-4b20-b5de-024705497d4a",
        "name": "Huseynli",
        "age": 26,
        "position": "Software developer",
        "companyId": "c9d4c053-49b6-410c-bc78-2d54a9991870",
        "company": null
    },
    {
        "id": "021ca3c1-0deb-4afd-ae94-2159a8479811",
        "name": "Kane Miller",
        "age": 35,
        "position": "Administrator",
        "companyId": "3d490a70-94ce-4d15-9494-5248280c2ce3",
        "company": null
    }
]

But when I call Http method it returns "The target location specified by path segment was not found" error. My request body is below:

[
    {
        "op": "replace",
        "path": "Employee",
        "value": [
            {
                "Name": "Farid",
                "Position": "Developer"
            }
        ]
    }
]

1 Answers1

0

Your employee object contains the id, name, position etc., so you need to provide the path of those parameters from the employee object.

Change your request body as below:

[
 {
    "op": "replace",
    "path": "/name",
    "value": "Farid"
 },
 {
    "op": "replace",
    "path": "/position",
    "value": "Developer"
 }
]
Adrian Mole
  • 49,934
  • 160
  • 51
  • 83
Amir Suhel
  • 33
  • 4