[英]Date parameter in controller action
I'm writing an API in dotnet core.我正在 dotnet 核心中编写 API。 I would like my controller action to take a date (eg. '2019-11-12') as a parameter, so the user doesn't have to provide a full
datetime
.我希望我的 controller 操作将日期(例如'2019-11-12')作为参数,因此用户不必提供完整的
datetime
。
I have the following:我有以下内容:
[HttpGet]
public async Task<ActionResult<IEnumerable<DogDto>>> GetDogsForKennelOnDate([FromQuery]string kennelName, [FromQuery]DateTime birthDate)
{...}
It seems to me that this is probably a common use case, but I haven't been able to find a solution on SO.在我看来,这可能是一个常见的用例,但我一直无法在 SO 上找到解决方案。 Can you help?
你能帮我吗?
EDIT:编辑:
Should I take a string
as parameter and then create a new DateTime
from this string
?我应该将
string
作为参数,然后从该string
创建一个新的DateTime
吗? Is this the right way to do it?这是正确的方法吗?
The reason why you get a compilation error for this:您为此收到编译错误的原因:
[HttpGet]
public async Task<ActionResult<IEnumerable<DogDto>>> GetDogsForKennelOnDate([FromQuery]string kennelName, [FromQuery]DateTime.Date birthDate)
{...}
is because the compiler is expecting you to define the data type for the birthDate parameter, but DateTime.Date is not a data type, but a property of the DateTime data type.是因为编译器期望你为birthDate参数定义数据类型,但DateTime.Date不是数据类型,而是DateTime数据类型的属性。
Now consider this, tested on .NET Core 2.2:现在考虑这个,在 .NET Core 2.2 上测试:
[HttpGet]
public async Task<ActionResult<IEnumerable<DogDto>>> GetDogsForKennelOnDate([FromQuery]string kennelName, [FromQuery]DateTime birthDate)
{...}
The query string parameter:查询字符串参数:
birthDate=2019-11-12
binds successfully to the birthDate parameter in the action (with 0 for the hours, minutes and seconds).成功绑定到操作中的birthDate 参数(小时、分钟和秒为0)。 So I think your assumption that the user has to provide the full date and time is wrong.
所以我认为你假设用户必须提供完整的日期和时间是错误的。
Note: The DateTime.Date property is itself a DateTime with the time value set to 12:00:00 midnight (00:00:00);注意:DateTime.Date 属性本身是一个 DateTime,其时间值设置为午夜 12:00:00 (00:00:00); see https://docs.microsoft.com/en-us/dotnet/api/system.datetime.date?view=netcore-2.2 .
请参阅https://docs.microsoft.com/en-us/dotnet/api/system.datetime.date?view=netcore-2.2 。
Edit: rather than making it impossible for the user to include the time in the query string parameter, you can instead ignore it (my preference as it is user-friendly):编辑:与其让用户无法在查询字符串参数中包含时间,不如忽略它(我的偏好是用户友好):
[HttpGet]
public async Task<ActionResult<IEnumerable<DogDto>>> GetDogsForKennelOnDate([FromQuery]string kennelName, [FromQuery]DateTime birthDate)
{
DateTime birthDateDate = birthDate.Date;
}
Or you could validate that the hours, minutes and seconds are 0, and create a model state error if they are not (not so user friendly).或者,您可以验证小时、分钟和秒是否为 0,并创建一个 model state 错误(如果不是用户友好型)。 You can do this using a custom validation attribute:
您可以使用自定义验证属性执行此操作:
public sealed class DateAttribute : ValidationAttribute
{
protected override ValidationResult IsValid(object value, ValidationContext validationContext)
{
if (validationContext == null)
{
throw new ArgumentNullException(nameof(validationContext));
}
if (!(value is DateTime))
{
return new ValidationResult($"Not a DateTime object ({value}).");
}
DateTime date = (DateTime)value;
if (date.Date != date)
{
return new ValidationResult($"Do not specify the time ({value}).");
}
return ValidationResult.Success;
}
}
Usage:用法:
[HttpGet]
public async Task<ActionResult<IEnumerable<DogDto>>> GetDogsForKennelOnDate([FromQuery]string kennelName, [FromQuery] [Date] DateTime birthDate)
{
...
}
This will now succeed validation:这现在将成功验证:
birthDate=2019-11-12
But this will fail:但这会失败:
birthDate=2019-11-12 11:22:33
However, this will still succeed:但是,这仍然会成功:
birthDate=2019-11-12 00:00:00
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.