在Web Api 2 Controller方法中访问路由和POST参数 [英] Accessing route and POST params in Web Api 2 Controller Method

查看:175
本文介绍了在Web Api 2 Controller方法中访问路由和POST参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个需要同时访问route和POST正文参数的控制器.但是,当我使用这种实现方式

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)
    {
        // ...
    }
}

message参数始终为null.

我如何访问所有apropos数据?

How can I access all the apropos data?

推荐答案

[FromBody]参数必须编码为值

[FromBody] parameters must be encoded as value

不要尝试这样做:

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

改为尝试

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

并使用这种代码对jquery进行POST请求:

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

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

有关更多详细信息,请参见以下链接 http ://encosia.com/using-jquery-to-post-frombody-parameters-to-web-api/

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

这篇关于在Web Api 2 Controller方法中访问路由和POST参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆