(C# ASP.NET Core) Minimal PUT WebApi for Updating a Record

An Http PUT request is used to update an existing record. For this, a client has to send the unique id of the record along with the entire updated record. Partial updates are not done with a PUT type of Web Api. Partial updates are, rather, supported by an HTTP PATCH request. But that is not the topic of this tutorial. Today we shall focus on the Http Put type of Web Api.
(Rev. 18-Jun-2024)

Categories | About |     |  


How is a PUT Web Api Created

A PUT Web Api is created with the MapPut function.

The first argument is the route pattern for the web api. The id of the record is a part of the route parameter. If your table has a composite primary key, then the route pattern can be designed to include more segments so that all the keys do get included as a part of the route.

The second argument is a function that receives the entire entity or object that has to be updated. This function also receives the id of the object. This id is the primary key as extracted from the route template.

Passing the id as a separate argument may seem redundant because primary key is, in any case, also a part of the object that is received as an argument. But, this takes care of the possibility where the primary key itself may need an updation. So, it is for that situation that id is an additional parameter.

Video Explanation (see it happen!)

Please watch the following youtube video:

Implementing a PUT Web Api in our Project

Let us now implement a PUT type of web api in the simple project that we are discussing for this chapter.

Open the solution explorer and locate the Program.cs file. Double click and open this file. Scroll to the part that you are seeing here.

// program.cs file 
// extract of the relevant part shown here 
// update a record of ID 
app.MapPut("/doctor/{id}", async (MyApiContext ctx, int id, Doctor doctor) =>

  var doc = await ctx.Doctors.FindAsync(id);

  if (doc is null)

    return Results.NotFound();


  doc.Name = doctor.Name;

  doc.Fees = doctor.Fees;

  await ctx.SaveChangesAsync();

  return Results.NoContent();


As explained earlier, we have used the MapPut function. The route is /doctor/{id}, and it contains the id as a route parameter.

Dependency injection provides us the DbContext. First the incoming id is used to locate the current record from the database. After that values are updated, and changes saved to the database.

Results.Content is used to produce 204 response as per the requirements of the Http PUT request.

Testing the WebApi with PostMan

Finally, let us test the Web Api with the Postman software.

First run the application to ensure that the local server starts running. Take note of the URL.

Next open the Postman software.

Choose the request type as PUT. Type the url containing an id of the record to be updated. We have set the id as 1.

The body of the request has to be in JSON format. So type all the updated properties of the new object in json format as you are seeing here (see the linked video).

Now hit the Send button.

We verify that we receive HTTP 204 - No Content as the response - which verifies that the MapPut WebApi has responded as expected! Thanks!

This Blog Post/Article "(C# ASP.NET Core) Minimal PUT WebApi for Updating a Record" by Parveen is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.