0

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 form Using JSON body Using JSON

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

ShefardPT
  • 379
  • 1
  • 2
  • 6

0 Answers0