When I tried to send DateTime object via form-data .NET-MVC binds it into DateTime object with the server's local timezone. But in the another case DateTime object is binded correctly if it has been received via JSON.
Using form-data
Using JSON body
a piece of Startup.cs where MvcCore is added
services.AddMvcCore(opt =>
{
opt.ModelMetadataDetailsProviders.Add(new EmptyStringModelBinder());
})
.SetCompatibilityVersion(CompatibilityVersion.Version_2_1)
.AddFormatterMappings()
.AddCacheTagHelper()
.AddJsonFormatters()
.AddCors()
.AddAuthorization();
The example model:
public class TestClass
{
public DateTime item { get; set; }
}
The example controller action:
[HttpPost]
public IActionResult TestPost([FromForm] TestClass item)
//public IActionResult TestPost([FromBody] TestClass item)
{
var currentCulture = Thread.CurrentThread.CurrentCulture;
var date = item.item;
var UTCDate = date.ToUniversalTime();
return Ok(UTCDate);
}
There is a test project about that issue Test project