ASP.NET MVC - REST Web API POST Method

REST Web API is the most vital component for sharing data across multiple devices; e.g., mobile devices, desktop applications or any website. The key element in developing & designing REST web API is to identify the type of methods that will eventually share the data. The most popularly used method types are GET & POST.
 
Today, I shall be demonstrating the creation of REST Web API POST type method using ASP.NET REST Web API platform.
 
 
Prerequisites
 
Following are some prerequisites before you proceed any further in this tutorial:
  1. Knowledge of REST Web API.
  2. Knowledge of ASP.NET MVC5.
  3. Knowledge of C# Programming.
The example code is being developed in Microsoft Visual Studio 2019 Professional.The sample sales data is taken randomly from the internet.
 
Let's begin now.
 
Step 1
 
Create a new Web API project and name it "RESTWebApiPostMethod".  
 
Step 2
 
Create "Controllers\WebApiController.cs" file.
 
Step 3
 
Create "Post" method inside "Controllers\WebApiController.cs" file and replace the following code in it.
  1.         #region POST api/WebApi  
  2.   
  3.         /// <summary>  
  4.         /// POST api/WebApi  
  5.         /// </summary>  
  6.         /// <param name="postData">Post data parameter</param>  
  7.         /// <param name="request">Request parameter</param>  
  8.         /// <returns>Return - Response</returns>  
  9.         public HttpResponseMessage Post([FromBody]JToken postData, HttpRequestMessage request)  
  10.         {  
  11.             // Initialization  
  12.             HttpResponseMessage response = null;  
  13.             RequestObj requestObj = JsonConvert.DeserializeObject<RequestObj>(postData.ToString());  
  14.             DataTable responseObj = new DataTable();  
  15.             string json = string.Empty;  
  16.   
  17. ...  
  18.                 // Loading Data.  
  19. ...  
  20.                 // Processing.  
  21. ...  
  22.                 // Settings.  
  23.                 json = JsonConvert.SerializeObject(responseObj);  
  24.                 response = Request.CreateResponse(HttpStatusCode.OK);  
  25.                 response.Content = new StringContent(json, Encoding.UTF8, "application/json");  
  26.   
  27. ...  
  28.             // Info.  
  29.             return response;  
  30.         }  
  31.  
  32.         #endregion 
In the above code, a simple post method has been created which deserialize the input JSON data, then process the request query and finally, prepare & send the response JSON packet. There are a  few things in the above code that need to be taken care of:
  1. The first point is the name of our target POST/GET type method. Know that in ASP.NET MVC platform, the advantage of writing REST Web API is that the platform take care of most of the things, the default names by convention in ASP.NET MVC REST Web API platform are "Post" for POST type method and "Get" for GET type method with method overloading with parameter variations. This is the reason developers do not explicitly need to write "HttpPost" data annotation/Attribute tag above POST type method to explicitly tell the platform that it is POST type method. However, if you need to change the default name of post/get method or you have a more complex number of methods then you can utilize Method Name Routing technique to achieve your target name, but, follow the convention and use post/get as prefix with your method name.

  2. You should understand how you want to send your input query data; i.e. via URL parameters or via JSON. In the above sample, I am sending input query data via JSON structure. For complex input query data JSON structure is recommended and for simple one or two parameters, the      URL form input data structure is preferred.

  3. You need to deserialize your input request query and map the input request structure to a JSON object mapper if you are using JSON as input request structure.

  4. Finally, you need to explicitly create your response packet to return JSON resultant data otherwise your direct JSON response might have slashes and improper string formatting making it dificult for your consumer application to consume the REST Web API.
Step 4
 
Let's test out REST Web API in action using REST Web API client. I am using Firefox plugin i.e. "RESTED". Execute the project and simply hit the REST Web API via URL directly as the REST Web API does not have any authorization and I will get the following response:
 
 
 
 

Conclusion

In this article, you learned to create REST Web API POST type method using ASP.NET REST Web API platform. You will also learn about default behavior that ASP.NET MVC platform offers in respect to creating POST/GET type methods. You learned about choosing the right input request query type form; i.e. either via URL parameter or via JSON. You then learned to deserialize the input request data using JSON object mapper technique. You also learned to properly create response packet with proper string formatting and finally, you learned to test REST Web API using any REST client to see your REST Web API in action before consumption.


Similar Articles