问题
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. It always arrives as query == null. There is Request object available too but I am not sure how to navigate through its members to reach my "Hellow World!".
回答1:
You need to pass name-value pairs from the client:
$.post("/DataAPI/messageProcessor"
, { query: "Hello World!" }
, function (data) {} );
Check jQuery.Post for more details.
回答2:
try this :
$.post("/DataAPI/messageProcessor", { 'query' : 'Hello World!' }, function (data) {
Handle(data);
}
});
回答3:
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;
}
来源:https://stackoverflow.com/questions/13920571/how-do-i-access-parameters-of-my-post-request-within-a-controller