I send UTC date in my request, for example
& EndDate = 2000-01-02T03% 3a04% 3a05.0060000Z
And my controller parameter binds it in a complex object, for example.
public async Task<DocumentsRequest> GetEchoFromUriDocumentsAsync( [FromUri] DocumentsRequest request) { return request; }
What I managed to find out is that my object after binding the parameter changes the date of my UTC to a local date + offset. Here is a snippet of my test case
[TestMethod] public void Should_generate_querystring_and_parameter_bind_correctly() { DocumentsRequest request = DocumentRequestBuilder.DocumentsRequest(); string queryString = ReflectionQueryStringConverter.ToQueryString(request); string baseUrl = "http://baseUrl/Test/EchoFromUriDocuments"; string uri = baseUrl + queryString; HttpResponseMessage response = _client.GetAsync(uri).Result; string outputString = response.Content.ReadAsStringAsync().Result; JsonSerializer<DocumentsRequest> serializer = new JsonSerializer<DocumentsRequest>(); DocumentsRequest output = serializer.DeserializeFromString(outputString); output.EndDate.Should().Be(request.EndDate); }
The above does not work because the output is:
2000-01-01T19: 04: 05.006-08: 00
But json serialization then truncates the offset and assumes this is a UTC date ... and, as you can see, the dates do not match the post-round-trip.
There is a similar question here, but not really an answer (the poster answered its own question)
Transferring UTC DateTime Results to the HttpGet Web API at Local Time
What is the right solution to solve this problem? I think the problem is that I want to bind the parameter to the parsing of the request as a UTC date. Is TypeConverter a way to go? Does anyone have a complete sample of how to do this? Should I use TypeConverter for all attributes in all my classes using DateTime? I was hoping for a global configuration.
Thanks.
source share