正在用 C# 解析从 HTTP POST 接收到的内容类型为 application/x-www-form-urlencoded 的 JSON 数据

Parsing JSON data in C# received from HTTP POST with content-type application/x-www-form-urlencoded

当内容类型为 JSON 时,我有一个 C# 端点正在接收和解析 HTTP Post。由于 Whosebug 的一些帮助,这非常有效。我错误地认为这是我需要的,但后来得知发布的内容类型是 application/x-www-form-urlencoded。 使用我现有的 API 控制器,我得到所有值的空值。 我需要做哪些不同的事情才能接收这种格式的负载?

当前 class 在 JSON:

时有效
namespace WebServiceTest1.Models
{

    public class Rootobject
    {
        public DateTime received { get; set; }
        public string authtype { get; set; }
        public string[] tags { get; set; }
        public Routingresults routingResults { get; set; }
        public string device_name { get; set; }
        public int errorcode { get; set; }
        public string source { get; set; }
        public string timestamp { get; set; }
        public string record_id { get; set; }
        public string data { get; set; }
        public int device_id { get; set; }

    }

    public class Routingresults
    {
        public int matchedRules { get; set; }
        public object[] errors { get; set; }
    }



}

在JSON时工作的电流控制器:

    namespace WebServiceTest1.Controllers
    {


        public class Konekt1Controller : ApiController
        {

            public IHttpActionResult Post(Rootobject dto)
            {
                //Do something here.  
                return Ok();
            }
}
}

感谢您的帮助。

**更新,这里是发布的内容:

FORM/POST 参数

有效载荷:{"received":“2016-05-13T20:43:17.845873”,"authtype":"otp","tags":[“SOCKETAPI", "SIMPLESTRING", "_DEVICE_37555_"], "device_name": "test (08921)", "errorcode": 0, "source": "1111111111111", "timestamp": "301", "data": "abc123Base64 encoded", "device_id": 37555} 钥匙: 属性:{"url": "http://requestb.in/15jclna1", "payload_is_json": true, "user_id": 2518, "hook_payload": "ALL"} 用户 ID:2518

我明白了。消息传入的方式全部存储在一个字符串中。所以而不是个人得到;设置;我原来的 post 中的值 我必须在我的 class 对象中获取字符串有效负载中的所有值,然后在控制器中将其解析出来。

public string payload { get; set; }