In earlier post we have learned about http request i.e., get and post. Now we will get more details about get http request with example. How this is used in .Net applications and what will be the action method parameters while receiving these requests on server side?
Get requests are used to get data from server side, data may be a complete model to bind on razor page or a partial view to directly shown on page. Get requests can be performed without parameter, with single parameter and with many parameters.
Get request without parameter
When we need data from server either without any condition or according to global/session value, then we need use this type of request. Syntax is already defined in previous post, so we will directly use this on razor page in .net core application.
$('#btnCallApi_WithoutParameter').click(function ()
{
$.get('/Home/GetWithoutParameter', function (res) {
alert(res);
});
});
Above code is sending request on GetWithoutParameter action in home controller without any query. 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 string value. Below is action method’s code:
public IActionResult GetWithoutParameter()
{
return Json("GetWithoutParameter");
}
Get request with one parameter
While we need data according to a query parameter, then we need to send parameter with get request like sending in the request below:
$('#btnCallApi_With_One_Parameter').click(function () {
$.get('/Home/Get_With_One_Parameter', { id: 3 }, function (res) {
alert(res);
});
});
Above code is sending request on Get_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:
public IActionResult Get_With_One_Parameter(Int32 id)
{
//get data according to this id parameter
return Json("Get_With_One_Parameter - id:" + id);
}
Get request with multi parameters
While we need data according to multi parameters, then we need to send those parameters with get request like sending in the request below:
$('#btnCallApi_multi_parameter').click(function () {
$.get('/Home/Get_With_Multi_Parameter', { id: 3, id2: 33, title: 'filter' }, function (res) {
alert(res);
});
});
Above code is sending request on Get_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:
public IActionResult Get_With_Multi_Parameter(Int32 id, int id2, string title)
{
//if you just want confirmation of api
return Json("Get_With_Multi_Parameter - id:" + id + " - id2:" + id2 + " - title:" + title);
}
In all three requests we have used URLs in string format. If we want to declare these URLs in html helper format as below:
<input type="button" value="API Without Parameter" id="btnCallApi_WithoutParameter" data-url="@Url.Action("GetWithoutParameter", "Home")" />
Beside this if we want to get this API URL from html button definition then we can assign this by any name with prefix data. And we can get this URL in jQuery by the either in-build data function or attr function.
$('#btnCallApi_WithoutParameter').click(function () {
//$(this) can be used with in an event
var apiUrl = $(this).data('url');
var apiUrl = $(this).attr('data-url');
});
In next post we will understand about post request. Post request have all formats as in get request but one extra i.e. we can pass model with post format.
1 comment