ASP.Net core MVC FromQuery DateTime in utc



我有一些类似的代码:

 [HttpGet("SearchStuff")]
    public IActionResult SearchStuff([FromQuery]DateTime from, [FromQuery]DateTime to)

我想确保我在UTC中收到如下:如果指定了时区,请转换为UTC如果未指定时区,则假设UTC并指定为UTC

我尝试这样从客户端发送utc(客户端在当地工作),并且正如我所期望的那样格式化,但不幸的是,DateTime将DateTime转换为服务器的本地时间。如果我使用ToString(" S")服务器接收正确的DateTime值,但不幸的是使用DateTimeKind.unspecified:

protected string Format(DateTime t)
{
    return t.ToUniversalTime().ToString("o");
}

也有此https://github.com/dotnet/aspnetcore/issues/11584提供解决方法

最新更新