[英]How do I access parameters of my POST request within a controller
Javascript: 使用Javascript:
$.post("/DataAPI/messageProcessor", { query: "Hello World!" }, function (data) {
Handle(data);
}
});
Controller: 控制器:
[System.Web.Http.AcceptVerbs("Post")]
[System.Web.Http.ActionName("messageProcessor")]
public ResponseModel messageProcessor(string query)
{
ResponseModel model=DoStuff(query);
return model;
}
How do I access query
from the controller. 如何从控制器访问query
。 It always arrives as query == null
. 它总是以query == null
到达。 There is Request
object available too but I am not sure how to navigate through its members to reach my "Hellow World!"
也有Request
对象可用,但我不确定如何导航其成员以到达我的"Hellow World!"
. 。
You need to pass name-value pairs from the client: 您需要从客户端传递名称/值对:
$.post("/DataAPI/messageProcessor"
, { query: "Hello World!" }
, function (data) {} );
Check jQuery.Post for more details. 检查jQuery.Post以获得更多详细信息。
try this : 尝试这个 :
$.post("/DataAPI/messageProcessor", { 'query' : 'Hello World!' }, function (data) {
Handle(data);
}
});
Thanks to a coworker. 多亏了一位同事。 Solution is as following: 解决方法如下:
public class QueryClass
{
public string query { get; set; }
}
public ResponseModel messageProcessor(QueryClass query)
{
ResponseModel model=DoStuff(query.query);
return model;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.