简体   繁体   English

使用Owin Self-Host ASP.Net API2无法接收带有正文中的数据而非查询字符串的POST请求

[英]Using Owin Self-Host ASP.Net API2 Can't Receive POST Request with data in body instead of query string

I been making a Bot to Receive notification from a Ark: Survival Evolved Game Server and Send them to the Steam account of the player using steam chat system ! 我一直在做一个机器人,以接收来自方舟:生存进化游戏服务器的通知,并使用Steam聊天系统将其发送到玩家的Steam帐户!

well, everything was done greatly with no problem, I tested my work using data sent from a query string, and it works and send the messages etc. 好吧,一切都做得很好,没有问题,我使用从查询字符串发送的数据测试了我的工作,并且可以正常工作并发送消息等。

but now that i check using the game server, it won't work. 但现在我检查使用游戏服务器,将无法正常工作。

The Problem, seems to be coming from sending the HTTP POST Request with body content instead of query string as my app will not receive them. 问题似乎来自发送带有正文内容而不是查询字符串的HTTP POST请求,因为我的应用程序不会接收到它们。

I have made a lot of examples to receive the POST requests but non seems to work, I hope someone here know how to fix this, I lost all hope as i been working on this for over 5 hours and even ask about it in C# SOChat. 我已经做了很多示例来接收POST请求,但似乎都行不通,我希望这里的人知道如何解决此问题,因为我从事此工作超过5个小时,甚至在C#SOChat中询问,我都失去了希望。 。

Here is my code : https://github.com/LuckyPed/Ark-Steam-Notifier-Console/blob/Post-Data/ArkSteamNotifier/ValuesController.cs 这是我的代码: https : //github.com/LuckyPed/Ark-Steam-Notifier-Console/blob/Post-Data/ArkSteamNotifier/ValuesController.cs

Non of these seems to work : 这些似乎都不起作用:

    public void Post1(string key, ulong steamid, string notetitle, string message)
    {
        if (!Program.settings.UnSub.Contains(steamid))
        {
            SteamID SID = new SteamID(steamid);
            Program.steamFriends.SendChatMessage(SID, EChatEntryType.ChatMsg, System.DateTime.UtcNow + " UTC ( GMT ) : " + notetitle);
            Thread.Sleep(1000);
            Program.steamFriends.SendChatMessage(SID, EChatEntryType.ChatMsg, message);
        }
    }

    public void Post([FromBody]string key,[FromBody]ulong steamid,[FromBody]string notetitle,[FromBody]string message)
    {
        if (!Program.settings.UnSub.Contains(steamid))
        {
            SteamID SID = new SteamID(steamid);
            Program.steamFriends.SendChatMessage(SID, EChatEntryType.ChatMsg, System.DateTime.UtcNow + " UTC ( GMT ) : " + notetitle);
            Thread.Sleep(1000);
            Program.steamFriends.SendChatMessage(SID, EChatEntryType.ChatMsg, message);
        }
    }

    public class MyFormData
    {
        public string Data { get; set; }
    }

    public void Post([FromBody]MyFormData formData)
    {
    }

    public void Post([FromBody]MyData formData)
    {
    }

    public class MyData
    {
        public string key { get; set; }
        public string steamid { get; set; }
        public string message { get; set; }
        public string notetitle { get; set; }
    }

    public void Post(string value)
    {
    }

    public void Post1([FromBody]string value)
    {
    }

Create an object to receive the data in the body: 创建一个对象以接收正文中的数据:

public class SteamData
{
    public string Key {get; set;}
    public ulong SteamId {get; set;}
    public string NoteTitle {get; set;}
    public string Message {get;set;}
}

Then change your Post method like this: 然后像这样更改您的Post方法:

public void Post([FromBody] SteamData data)
{
    if (!Program.settings.UnSub.Contains(steamid))
    {
        SteamID SID = new SteamID(data.SteamId);
        Program.steamFriends.SendChatMessage(SID, EChatEntryType.ChatMsg, System.DateTime.UtcNow + " UTC ( GMT ) : " + data.NoteTitle);
        Thread.Sleep(1000);
        Program.steamFriends.SendChatMessage(SID, EChatEntryType.ChatMsg, data.Message);
    }
}

Then when the post happens, the data can more easily be in the body of the request. 然后,当发布发生时,数据可以更轻松地放在请求的正文中。 WebAPI has difficulties retrieving more than one parameter with the [FromBody] attribute. WebAPI很难通过[FromBody]属性检索多个参数。 You can do it, but it's harder than it is worth. 您可以做到,但是它比价值更难。

Hope that helps. 希望能有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM