API端点上来自Web发布的数据模型为空(C#ASP.NET WEB API)

我正在尝试从Mattermost的自定义斜杠命令使用数据。

根据文档,它将发送以下post方法:

POST /slash-command HTTP/1.1
Host: example.com
User-Agent: Go-http-client/1.1
Content-Length: 313
accept: application/json
Authorization: Token okwexkjpe7ygb8eq1ww58t483w
Content-Type: application/x-www-form-urlencoded
accept-Encoding: gzip

channel_id=jux16pkewjrkfj3ehep1psxyxc&
channel_name=town-square&
command=%2Ftest&
response_url=http%3A%2F%2Flocalhost%3A8065%2Fhooks%2Fcommands%2Fxbrkb8p393gjpq5cawei7npije&
team_domain=test&
team_id=carya1qs77bemjup96ff538snh&
text=asd&
token=okwexkjpe7ygb8eq1ww58t483w&
user_id=aoa1agao6t8fmx3ikt1j9w5ybw&
user_name=somename
channel_mentions=["saepe-5","aut-8"]
channel_mentions_ids=["r3j6sby343fpfdxcbwqg95rfsa","ehjj46yk7ifptr5bpfb966s6mc"]
user_mentions=["aaron.peterson","aaron.medina"]
user_mentions_ids=["q5s3b7xzgprp5eid8h66j9epsy","czwmumrmw7dfxecww7qibkkoor"]

我只需要文本和user_id,所以我创建了这样的视图模型:

public  class MattermostSlashCommandViewModel
    {
        [JsonProperty("user_id")]
        public string user_id { get; set; }

        [JsonProperty("text")]
        public string text { get; set; }

    }

然后在API端点上,我将其用作参数

        [HttpPost]
        [actionName("receivedata")]
        public void ReceiveData([FromBody] MattermostSlashCommandViewModel mattermostSlashComandmodel)
        {
         string test= mattermostSlashComandmodel.user_id;
        }

命中端点,但是user_id或任何其他参数为空。

还尝试了[FromUri],只是将所有对象记为单独的参数,但这也不起作用。

我错过了什么吗?在正确的方向上轻推将不胜感激。

aaassdqw 回答:API端点上来自Web发布的数据模型为空(C#ASP.NET WEB API)

暂时没有好的解决方案,如果你有好的解决方案,请发邮件至:iooj@foxmail.com
本文链接:https://www.f2er.com/2331979.html

大家都在问