Accessing route and POST params in Web Api 2 Controller Method

做~自己de王妃 提交于 2019-12-13 04:16:17

问题


I have a controller that needs access both route and POST body parameters. However when I use this implementation

public class MessageController : ApiController
{
    [Route( "Data/Message/{apiKey}/{userId}" )] 
    [HttpPost]
    public Message Post( Guid apiKey, string userId, [FromBody] string message)
    {
        // ...
    }
}

the message argument is always null.

How can I access all the apropos data?


回答1:


[FromBody] parameters must be encoded as value

don't you try to do this:

public Message Post( Guid apiKey, string userId, [FromBody] string message)
{
    // ...
}

try this instead

 public Message Post( Guid apiKey, string userId, [FromBody] string value)
 {
    // ...
 }

and use this kind of code to do POST request with jquery:

  $.post('YourDomain/Data/Message/{apiKey}/{userId}', { '': value });

for more detail, here is the link http://encosia.com/using-jquery-to-post-frombody-parameters-to-web-api/



来源:https://stackoverflow.com/questions/25771329/accessing-route-and-post-params-in-web-api-2-controller-method

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