I would like to change the following api controller method from GET to POST request. I have made the changes on the header however I am getting a the following error, whenever I call api/list?tag=89077&name=prest :
{"$id":"1","Message":"The requested resource does not support http method 'GET'."}
[HttpPost]
public HttpResponseMessage Post([FromBody] Query query)
{
IQueryable<data_qy> Data = null;
if (Data == null)
Data = db.data_qy;
if (query.endDate != null)
{
Data = Data.Where(c => c.UploadDate <= query.endDate);
}
if (query.startDate != null)
{
Data = Data.Where(c => c.UploadDate >= query.startDate);
}
Data = Data.OrderByDescending(c => c.UploadDate);
var data = Data.ToList();
if (!data.Any())
{
var message = string.Format("No data found");
return Request.CreateErrorResponse(HttpStatusCode.NotFound, message);
}
return Request.CreateResponse(HttpStatusCode.OK, data);
}
*Edit
public class Query
{
public string name { get; set; }
public string tag { get; set; }
public Nullable<DateTime> startDate { get; set; }
public Nullable<DateTime> endDate { get; set; }
}
$(document).ready(function () {
var data = {name: "", tag: ""};
var url = '/api/list/Post';
$.ajax({
url: url,
data: JSON.stringify(data),
type: 'POST',
contentType: "application/json",
});
});
I am little unsure, if the (data), should be calling data variable in the javascript or calling query object. I also would like to ask, I can test POST method as I test GET methods, such as api/list/name=pres&tag=679.
I have tested the above code, by passing querystring as -- api/list/name=pres&tag=679. However, it still gives me the same error.
Please advice, if I am missing something or if I am using an incorrect approach.
Do I need to change code in my global.aspx or somewhere else. Please advise.
Many thanks.