【问题标题】:Pass datetime in the webapi post request在 webapi 发布请求中传递日期时间
【发布时间】:2023-04-03 20:15:01
【问题描述】:

您好,我有一个如下的日期时间,我已将其转换为字符串,但它没有通过

     lastRefreshDateTime1 = "2015-07-09T08:38:49-07:00";
await client.PostAsync($"{_printApiUrl}pdf/GenerateAndEmailZipOfPDFs/${"{" + lastRefreshDateTime1 + "}"}", content, cancellationToken);

但是当我通过测试时,它可以正常工作,如下所示。

 lastRefreshDateTime1  = "test"; 

然后它通过。我做错了什么?

【问题讨论】:

  • but it does not pass through 什么意思?你得到错误或什么?
  • 不,我没有收到任何错误,但 [Route("api/pdf/GenerateAndEmailZipOfPDFs/{lastRefreshDateTime}")] 在仅通过日期时间时不会被调用
  • 我建议你对日期时间值进行 URL 编码
  • 我该怎么做?
  • client.PostAsync($"{_printApiUrl}pdf/GenerateAndEmailZipOfPDFs/${"{" + HttpUtility.UrlEncode(lastRefreshDateTime1) + "}"}", content, cancellationToken); 这样吗?

标签: c# asp.net-core


【解决方案1】:

这是一个简单的演示:

1.调用api:

var lastRefreshDateTime1 = "2015-07-09T08:38:49-07:00";
var client = new HttpClient();                      
await client.PostAsync("http://localhost:57646/api/pdf/GenerateAndEmailZipOfPDFs/"+ lastRefreshDateTime1,content,cancellationToken);

2.Api:

[HttpPost]
[Route("api/pdf/GenerateAndEmailZipOfPDFs/{lastRefreshDateTime}")]
public IActionResult Privacy([FromBody]string data,[FromRoute]DateTime lastRefreshDateTime)
{
    //...
}

3.结果:

【讨论】:

    猜你喜欢
    • 2018-11-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-20
    • 2021-03-14
    • 1970-01-01
    相关资源
    最近更新 更多