HTTP post request .Net

In earlier post we have learned about http request i.e., get and post. Now we will get more details about post http request with examples. How this is used in .Net applications and what will be the action method parameters while receiving these requests on server side?

Post requests are used to send data to server and receive data accordingly, data may be a complete model to bind on razor page or a partial view to directly shown on page. Post requests can be performed without parameter, with single/many parameters and object parameter.

Post request without parameter: This is same like get request without parameter and we don’t need to use it as post request but we can.

Post request with one parameter

When we need to send some value to server and receive data accordingly. Below is an example to use this request:

$('#btnCallApi_With_One_Parameter').click(function () 
{
    $.post('/Home/Post_With_One_Parameter', { id: 3 }, function (res) {
        alert(res);
    });
});

Above code is sending request on Post_With_One_Parameter action in home controller with id value. This request will be sent on button click event. From server side we are returning simple string value and after completing this request we will show an alert with returning id value.

Below is action method’s code:

[HttpPost] //annotation to define this is post request
public IActionResult Post_With_One_Parameter(Int32 id)
{
    //get data according to this id parameter
    return Json("Post_With_One_Parameter - id:" + id);
}

Post request with multi parameters

While we need to send multiple data to server, then we need to send those parameters with post request like sending in the request below. It is also same like get request with multi parameters. But if parameter value is too long then we have to use post request.

$('#btnCallApi_multi_parameter').click(function () 
{
   $.post('/Home/Post_With_Multi_Parameter', { id: 3, id2: 33, title: 'filter' }, function (res) {
       alert(res);
   });
});

Above code is sending request on Post_With_Multi_Parameter action in home controller with id, id2 and title parameter value. This request will be sent on button click event. From server side we are returning simple string value and after completing this request we will show an alert with returning all three values.

Below is action method’s code:

[HttpPost]
public IActionResult Post_With_Multi_Parameter(Int32 id, int id2, string title)
{
    //if you just want confirmation of api
    return Json("Post_With_Multi_Parameter - id:" + id + " - id2:" + id2 + " - title:" + title);
} 

Post request with object parameter

This type of request is used to send a model object to server side and this is not possible with get request. For this we need to create a json object into jquery and then send to the request. Just look out following code and you can use it easily in .net application.

$('#btnCallApi_with_objectparameter').click(function () 
{
    var obj = {
        name: 'progjs',
        age: 1,
        group: 'website',
        department: 'it'
    };
    $.post('/Home/Post_With_Object_Parameter', { param: obj}, function (res) {
        alert(res);
    });
});

We have sent a model to controller’s action method and to receive that model as it is we need a class with same properties as in this json object. These properties can’t be changed in case also like if these are in lowercase in json then in this model these should be in lowercase. If we will change the case then you can’t receive values assigned.

[HttpPost]
public IActionResult Post_With_Object_Parameter(JQFormModel param)
{
      //if you just want confirmation of api
      return Json("Post_With_Object_Parameter - object: " + param.name + ", " + param.age + ", " + param.group + ", " + param.department);
}

public class JQFormModel
{
    public string name { get; set; }
    public int age { get; set; }
    public string group { get; set; }
    public string department { get; set; }
}

Keep in mind that this JQFormModel class should not be defined under this action method as i have written. This should be in different file or after controller’s class scope braces.

In next post we will understand how we can return a partial view from action method and load on our page.

By Subhash Sharma

I have an interest in development and sharing my knowledge here.

1 comment

Leave a comment

Your email address will not be published. Required fields are marked *