question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

DateTimeOffset modelbinding from form is always set to utc in net7

See original GitHub issue

Is there an existing issue for this?

  • I have searched the existing issues

Describe the bug

Hello,

I have the following model that is used as input of a controller method:

public class TestController : ControllerBase
{
    [HttpPost]
    public DateTimeOffset Post([FromForm] InputModel model)
    {
        return model.StartDate;
    }
}

public class InputModel
{
    public DateTimeOffset StartDate { get; set; }
}

When I sent the string ‘2022-11-29 10:00’ in NET6, the local timezone offset (+1 in my case) is used for the DateTimeOffset. However, in NET7, the offset is always 0.

Expected Behavior

I expect the datetimeoffset is parsed with the local timezone offset, if none is provided in the input string.

Steps To Reproduce

https://github.com/yannicsmeets/datetimeoffset-demo

The main branch is net6. In the net7 branch, the target framework is set to net7.0

Exceptions (if any)

No response

.NET Version

7.0.100

Anything else?

No response

Issue Analytics

  • State:closed
  • Created 10 months ago
  • Comments:10 (8 by maintainers)

github_iconTop GitHub Comments

2reactions
virtualccacommented, Dec 22, 2022

I did some investigation and I believe the introduction of the TryParseModelBinder, evaluated earlier than the SimpleTypeModelBinder, caused the issue.

When binding using the TryParseModelBinder the DateTimeStyles is set to DateTimeStyles.AssumeUniversal | DateTimeStyles.AllowWhiteSpaces while the binding using the SimpleTypeModelBinder, call to DateTimeOffsetConverter, will set it to DateTimeStyles.None causing the behavior mentioned above.

Referring to the statement here, I found that as long as the TryParseModelBinderProvider is remove first and then added, it will be normal

like this

mvcBuilder.AddMvcOptions(options =>
  options.ModelBinderProviders.RemoveType<TryParseModelBinderProvider>();
  options.ModelBinderProviders.Add(new TryParseModelBinderProvider()
);
0reactions
mitchdennycommented, Mar 13, 2023
Read more comments on GitHub >

github_iconTop Results From Across the Web

How to configure model binding to get UTC DateTime via ...
When I tried to send DateTime object via form-data .NET-MVC binds it into DateTime object with the server's local timezone.
Read more >
DateTimeOffset.UtcDateTime Property (System)
It converts the date and time of the current DateTimeOffset object to Coordinated Universal Time (UTC). The conversion is performed by subtracting the...
Read more >
Converting between DateTime and DateTimeOffset
Based on its offset, it determines whether the DateTimeOffset value is a UTC time, a local time, or some other time and defines...
Read more >
DateOnly in .NET 6 and ASP.NET Core 6
Use DateTime or DateTimeOffset and make sure TimeOfDay is Zero. And pay extra care when doing cross-timezone conversations.
Read more >
Custom Model Binding in Asp.net Core, 2: Getting Time + ...
Once the form is posted a custom model binder combines both the DateTime and the client Time Zone Offset into an unique DateTimeOffset....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found