在 Post 请求中发送 DateTime

Send DateTime in Post Request

您好,我有 C# (Xamarin) 应用程序和 .net API。我如何 post 我的 .Net API 的日期时间?

.Net API

目前我有:

        var formContent = new FormUrlEncodedContent(new[]
        {
            new KeyValuePair<string, string>("Email", u.Email),
            new KeyValuePair<string, string>("Password", u.Password),
            new KeyValuePair<string, string>("ConfirmPassword", u.ConfirmPassword),
            new KeyValuePair<string, string>("Gender", u.Gender),
            new KeyValuePair<string, string>("DateOfBirth", u.DateOfBirth)
        });


        HttpResponseMessage response = await client.PostAsync("/api/Account/Register", formContent);

所有字符串都可以正常工作,但我无法弄清楚 DateTime。我猜是:

       KeyValuePair<string, DateTime>("DateOfBirth", u.DateOfBirth)

但这不起作用。如果我将 DateTime 转换为字符串,服务器上的日期将显示为 01/01/1900,这也不正确。

发送报价怎么样?

DataTime 转换为报价并发送:

var ticks = DateTime.UtcNow.Ticks;

我实际上最终序列化了具有我想要的所有属性的对象 post,这似乎有效。

string postBody = JsonConvert.SerializeObject(u);

HttpResponseMessage response = await client.PostAsync("/api/Account/Register", new StringContent(postBody, Encoding.UTF8, "application/json"));