如何在 .net web api 中将 DateTime 对象作为 json 传递

Ary*_*a11 3 asp.net-web-api asp.net-core

我有一个包含 DateTime 类型的模型:

    public class ToDo
    {
        public int id { get; set; }
        public int parentId { get; set; }
        public string note { get; set; }
        public DateTime due { get; set; }
        public ToDo(int id, int parentId, string note, DateTime due)
        {
            this.id = id;
            this.parentId = parentId;
            this.note = note;
            this.due = due;
        }
    }
Run Code Online (Sandbox Code Playgroud)

我为这个类创建了一个控制器来通过 api 发送我的帖子请求。但我不知道如何将 DateTime 类型绑定到 json 我已经尝试了一个具有以下正文的请求,但没有成功:

    {"parentId":1,"note":"hello world","due":{"year":2017,"month": 11,"day":25}}
Run Code Online (Sandbox Code Playgroud)

我应该如何发布 DateTime 类型?

Ary*_*a11 7

显然,您可以这样做的方法之一是:

{"due": "2017-11-01T00:00:00"}
Run Code Online (Sandbox Code Playgroud)

这实际上是一个简单的问题,但如果您想确定如何对未知对象类型格式发出正确的 post 请求,最好发送一个空主体的对象以查看默认值。