Http Query Parameters in UTC in AspNet Core

diegosasw

I'm having an issue that I cannot figure out.

I try to deal ALWAYS with UTC DateTime at server side.

I have an AspNetCore Mvc application with an endpoint that accepts queries that may include DateTimes. I want Mvc to understand that these dates are already in UTC and not to transform them "again".

My system is in Spain, (UTC +2)

If I send an http request to my localhost server like this:

http://localhost:50004/api/Resources?appliesOn=2018-06-30T18:00:00.000Z

I want to have the deserialized datetime as UTC representing the same date as:

DateTime.SpecifyKind(new DateTime(2018, 6, 30, 18, 0, 0), DateTimeKind.Utc)

But I can see that Mvc always transforms the date 2018-06-30T18:00:00.000Z into two hours later: 2018-06-30 20:00:00

I have tried to tell Mvc to use UTC json serializer/deserializer but nothing changes:

services
  .AddMvc()
  .AddJsonOptions(options =>
  {
      options.SerializerSettings.DateTimeZoneHandling = DateTimeZoneHandling.Utc;
  });

Is there a way to send query parameters in an http GET request already as a representation of UTC datetime? I understood a Date in ISO 8601, if it has a suffix Z it means "zero-offset" which should be interpreted already as UTC date time. Why Mvc is then transforming this and adding 2 hours offset?

Any clarification would be much appreciated!

PS: This is my endpoint, nothing special as you can see:

[HttpGet("")]
public IActionResult GetResources()
{
    var displayUri = Request.GetDisplayUrl();
    var requestUri = new Uri(displayUri);
    var filter = _filteredRequestFactory.Create(requestUri);
    var resources = _myProjection.GetResourcers(filter);
    return Ok(resources);
}
diegosasw

Thanks to another question in StackOverflow I found out that the reason this happens is because AspNetCore Mvc deserializer does not even use Json.Net deserializer in a GET http request to deserialize the query parameters.

Therefore, the following request: http://localhost:50004/api/DateTests?date=2018-06-15T18:00:00.000Z

would be captured by my endpoint:

[HttpGet("")]
public IActionResult GetDate(DateTime date)
{
    return Ok(date.ToString("o"));
}

and return the date in ISO 8601 format as follows: 2018-06-15T20:00:00.0000000+02:00

It deserializes the query parameter as if it was a local time date, and it applies the UTC + 2 (because the app is in Spain).

I needed a way to tell AspNet Core Mvc deserializer to understand that the query parameter that looks like a date, should be treated already as an UTC date and not modified it when deserializing.

The answer was to create a custom model binder and apply it either to that endpoint or globally.

I found a good implementation and after adding that model binder provider and model binder,

my endpoint now returns for the request with ?date=2018-06-15T18:00:00.000Z :

2018-06-15T18:00:00.0000000 as a DateTime of UTC kind.

If I pass a ?date=2018-06-11T18:00:00+0100 it will be retrieved as a local time kind and the result would be: 2018-06-11T19:00:00.0000000+02:00

as desired

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

Passing list of enum values as HTTP query parameters

分類Dev

Passing list of enum values as HTTP query parameters

分類Dev

NLog AspNet Core 5.0

分類Dev

AspNet Core CookieAuthentication with injected SessionStore

分類Dev

Bind query parameters to a model in ASP.NET Core

分類Dev

passing optional query string parameters to http service call

分類Dev

Use Authorization middleware instead of AuthorizationAttribute ASPNET Core

分類Dev

Model binding not working in aspnet core web api

分類Dev

AspNet Core3ID構成

分類Dev

AspNet Core2タイプ「Microsoft.AspNetCore.Http.HttpContext」セッションのサービスがありません

分類Dev

aspnet-core-ad-authenticationプロジェクトHTTPエラー502.5-プロセスの失敗

分類Dev

AspNet Core using in memory repo for data protection when running in IIS

分類Dev

AspNet Core using in memory repo for data protection when running in IIS

分類Dev

AspNet Core using in memory repo for data protection when running in IIS

分類Dev

AspNet Core DI:TryAddとAddの使用法

分類Dev

How to validate user agains policy in code in aspnet core?

分類Dev

Swashbuckle aspnet core 2.0 Swaggerconfig.cs not created

分類Dev

Using Google OAuth to secure web services in aspnet core

分類Dev

aspnet core 2.2 web app environment variables not changing in docker

分類Dev

AspNet Core - input/output JSON serialization settings at Controller Level

分類Dev

Dotnet Core Aspnet 1.1用のSimple(st)Dockerfile

分類Dev

Parse query parameters with regexp

分類Dev

Use parameters for MySqlCommand query

分類Dev

Microsoft.Net.HttpとMicrosoft.AspNet.WebApi.Client

分類Dev

Angular $ httpを使用してaspnet webapiで取得

分類Dev

Laravel Eloquent query with optional parameters

分類Dev

301 redirect with query parameters and ~ in URL

分類Dev

JpaRepository native query not detecting parameters

分類Dev

RSQLite parametrised query with vectors as parameters

Related 関連記事

  1. 1

    Passing list of enum values as HTTP query parameters

  2. 2

    Passing list of enum values as HTTP query parameters

  3. 3

    NLog AspNet Core 5.0

  4. 4

    AspNet Core CookieAuthentication with injected SessionStore

  5. 5

    Bind query parameters to a model in ASP.NET Core

  6. 6

    passing optional query string parameters to http service call

  7. 7

    Use Authorization middleware instead of AuthorizationAttribute ASPNET Core

  8. 8

    Model binding not working in aspnet core web api

  9. 9

    AspNet Core3ID構成

  10. 10

    AspNet Core2タイプ「Microsoft.AspNetCore.Http.HttpContext」セッションのサービスがありません

  11. 11

    aspnet-core-ad-authenticationプロジェクトHTTPエラー502.5-プロセスの失敗

  12. 12

    AspNet Core using in memory repo for data protection when running in IIS

  13. 13

    AspNet Core using in memory repo for data protection when running in IIS

  14. 14

    AspNet Core using in memory repo for data protection when running in IIS

  15. 15

    AspNet Core DI:TryAddとAddの使用法

  16. 16

    How to validate user agains policy in code in aspnet core?

  17. 17

    Swashbuckle aspnet core 2.0 Swaggerconfig.cs not created

  18. 18

    Using Google OAuth to secure web services in aspnet core

  19. 19

    aspnet core 2.2 web app environment variables not changing in docker

  20. 20

    AspNet Core - input/output JSON serialization settings at Controller Level

  21. 21

    Dotnet Core Aspnet 1.1用のSimple(st)Dockerfile

  22. 22

    Parse query parameters with regexp

  23. 23

    Use parameters for MySqlCommand query

  24. 24

    Microsoft.Net.HttpとMicrosoft.AspNet.WebApi.Client

  25. 25

    Angular $ httpを使用してaspnet webapiで取得

  26. 26

    Laravel Eloquent query with optional parameters

  27. 27

    301 redirect with query parameters and ~ in URL

  28. 28

    JpaRepository native query not detecting parameters

  29. 29

    RSQLite parametrised query with vectors as parameters

ホットタグ

アーカイブ