(C# ASP.NET Core) Minimal POST WebApi for Creating a new Record

A web api of the POST type is used to create a new record. For example, if we have to create a new record of a doctor, then we have to use a POST api to send the relevant fields for creating a record.

Categories | About |     |  

Parveen,

How is a Post Api Created

A POST Web Api is created with the MapPost function.

The first argument is a route pattern for the URL for the Api, and the second a function that accepts a parameter containing the record to be created. For example, if a record of type Doctor is to be created, then the parameter would be of the type Doctor. Another alternative is to pass each property as a parameter and then assemble an instance out of those properties. But this is rarely done.

Video Explanation (see it happen!)

Please watch the following youtube video:

Adding a POST WebApi to our Project

Let us now add a WebApi of the POST type to the project that we are doing in the past few tutorials of this chapter.

Open the solution explorer and locate the Program.cs file. This is the file in which we are serially adding our minimal Web Api. Double click and open this file. Scroll to the part that you are seeing here.

// extract of our Program.cs file 

// . . . 
// see the downloads for the whole project 
// only the relevant part of program.cs file shown 

// MapPost 
app.MapPost("/doctor", async (MyApiContext ctx, Doctor doctor) =>
{

  ctx.Doctors.Add(doctor);

  // success assumed for tutorial purposes 
  await ctx.SaveChangesAsync();

  // 201(Created) see HTTP Protocol 
  // https://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html 
  return Results.Created($"/doctor/{doctor.Id}", doctor);

  // if no url possible then send either of these 
  // return Results.Ok(); 

  // return Results.NoContent(); 

});


// . . . 
// see the downloads for the whole project 
// only the relevant part of program.cs file shown 

As explained earlier, we have used the MapPost function to describe the route to our web api.

The first parameter is the route pattern, and the second parameter is the delegate that executes when a POST request is matched. Dependency injection provides a DbContext, which is used to add a record to the database.

Assuming success, a response of 201 is sent with the Results.Created function. The first parameter of this function must be a URL that can be used to access the new record, and the second is the record that has been created. This is a requirement of the HTTP Protocol. Please visit w3.org for more on this protocol.

If a URL for the new record is not available, then we can send 200 OK or 204 No Content. In case you ever need to do that, then the relevant methods have been commented in the code that you see here.

Testing the WebApi with Postman

Finally, let us test our web api with Postman.

First run our web application to start the server. This will also help us know the port and url to the application.

Next open the Postman software (please refer the attached video).

Select the request type as POST.

Type the URL as /doctor.

The mode of data exchange in minimal webapi is through JSON strings. So the data to post MUST be in a JSON String.

So, select Body and then select JSON format, and after that type the json for the new record: {"Name":"Doc 3","Fees":200}


        {"Name":"Doc 3","Fees":200}

Hit the Send button!

We verify that the response is 201 Created and the json for the newly created recorded can be seen in the response from the server.

This verifies that we have done our POST api with success! Thanks!


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