简体   繁体   English

如何在控制器中访问POST请求的参数

[英]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.

相关问题 如何在asp net core 5 controller(用于POST方法)中阻止无效的json请求参数 - How do I block invalid json request parameters in asp net core 5 controller (for POST method) 如何在控制器的相同post方法内订阅并引发事件? - How do I subscribe to and raise an event within the same post method of my controller? 请求结束后,我该如何在控制器中执行某些操作? - How can I do something within the controller at the end of the request? 如何在控制器或视图中访问 LoginPath? - How do I access LoginPath within a controller or a view? 如何获取要发布到 controller 的复选框值列表 - How do I get a list of checkbox values to post to my controller 我如何确认控制器中的操作删除(POST)? - How do i confirm the action delete(POST) in my controller? 如何从C#服务器中的POST(或其他HTTP)请求访问参数? - How can I access parameters from a POST (or other HTTP) request in a C# server? 在.Net core 2.0中,当我向控制器发送POST请求时,数据未绑定到我使用的对象。 如何绑定这些数据? - In .Net core 2.0 when I send a POST Request to my controller the data does not bind to the object I have used. How do I bind this data? 如何将多个参数传递给控制器​​? - How do I pass in multiple parameters to a controller? 为什么我的post参数没有传递给我的控制器? - Why are my post parameters not making it to my controller?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM