问题
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