How do I access parameters of my POST request within a controller

元气小坏坏 提交于 2020-01-23 12:38:27

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!