简体   繁体   English

将 Datetime 参数传递给 Web Api 的最佳实践

[英]Best practice for passing Datetime parameter to Web Api

I have an Api call like whose signature is like below我有一个 Api 调用,其签名如下

[Route("TaskToDo/{userId}/{dateFrom}/{dateTo}")]
public async Task<IActionResult> GetTaskToDo(string userId, DateTime dateFrom, DateTime dateTo)

I can't hit the api call in swagger when i enter date like '13-08-2020' because it assumes that it will expect in DD-MM-yyyy.当我输入像“13-08-2020”这样的日期时,我无法在 swagger 中点击 api 调用,因为它假定它会在 DD-MM-yyyy 中预期。

What is the best approach to solve this issue.. should i change the method signature and take the datetime in string解决此问题的最佳方法是什么..我应该更改方法签名并采用字符串中的日期时间吗

or或者

it should be defined that the api is expected to receive the date in a specific format.应该定义 api 预计会以特定格式接收日期。

You can send the date in ISO Format (2020-08-13T00:00) JsonSerializer can covert this to a valid date time.您可以以 ISO 格式 (2020-08-13T00:00) 发送日期,JsonSerializer 可以将其转换为有效的日期时间。

To send the date in the URL you have to encode it for example:要在 URL 中发送日期,您必须对其进行编码,例如:

2020-08-13T00:00 -> 2020-08-13T00%3A00 2020-08-13T00:00 -> 2020-08-13T00%3A00

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

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