如何调用多个参数的post方法? [英] How to call post method for multiple parameters?
问题描述
我想将以下api控制器方法从GET更改为POST请求。我已经对标题进行了更改但是每当我调用api / list时都会出现以下错误?tag = 89077& name = perst:
{$ id:1,消息:请求的资源不支持http方法'GET'。}
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) // If no tags or name is being queried, apply filters to the whole set of products
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",
});
});
我不太确定,如果(数据),应该在ja中调用数据变量vascript或调用查询对象。我也想问一下,我可以在测试GET方法时测试POST方法,例如api / list / name = pres& tag = 679。
我测试了上面的代码,通过传递查询字符串为 - api / list / name = pres& tag = 679。但是,它仍然给我同样的错误。
如果我遗漏了某些东西,或者我使用了错误的方法,请提供建议。
我需要在global.aspx或其他地方更改代码。请指教。
非常感谢。
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.
推荐答案
id:1,消息: 请求的资源不支持http方法'GET'。}
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) // If no tags or name is being queried, apply filters to the whole set of products
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';
(document).ready(function () { var data = {name: "", tag: ""}; var url = '/api/list/Post';
.ajax({
url:url,
data: JSON .stringify(data),
类型:' POST',
contentType: application / json,
});
});
.ajax({ url: url, data: JSON.stringify(data), type: 'POST', contentType: "application/json", }); });
我不太确定,如果(数据),应该在javascript或调用查询对象中调用数据变量。我也想问一下,我可以在测试GET方法时测试POST方法,例如api / list / name = pres& tag = 679。
我测试了上面的代码,通过传递查询字符串为 - api / list / name = pres& tag = 679。但是,它仍然给我同样的错误。
如果我遗漏了某些东西,或者我使用了错误的方法,请提供建议。
我需要在global.aspx或其他地方更改代码。请指教。
非常感谢。
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.
这篇关于如何调用多个参数的post方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!