I’m facing an issue with the DateOnly type in a .NET 6 API where the date format is not mapped correctly when passed via a query parameter. The expected date format is dd.MM.yyyy
, which is standard for Norway, but it’s being interpreted as MM.dd.yyyy
instead.
Here’s the relevant part of my controller:
[HttpGet("data")]
public Task<IActionResult> GetData([FromQuery] DateOnly date)
{
return Task.FromResult<IActionResult>(Ok($"Day {date.Day} Month {date.Month} Year {date.Year}"));
}
And here’s an example request:
http://localhost:7053/data?date=01.04.2024
However the output is:
Day 04 Month 01 Year 2024
This indicates that the date is being interpreted as Jauary 4th, 2024
, rather than April 1st, 2024
. The application’s culture is set to Norway, where the standard date format is dd.MM.yyyy
. I’m looking for a solution that specifically works with the DateOnly type, without resorting to using DateTime. I am aware that DateTime could be used as a workaround, but I need a solution that adheres to using DateOnly.
Has anyone encountered this issue or know of a way to ensure the correct date format is used with DateOnly in .NET 6?
To ensure that the DateOnly
parameter in your C# controller receives dates in the dd.MM.yyyy
format when sent from the client, you need to configure model binding in your ASP.NET Core application. By default, ASP.NET Core interprets dates based on the culture settings of the server, which might be causing the mix-up between day and month in your scenario.
Here’s how you can ensure the DateOnly
parameter accepts dates in the specified format. You will need to create a custom model binder that explicitly parses dates using the dd.MM.yyyy
format.
public class DateOnlyModelBinder : IModelBinder
{
public Task BindModelAsync(ModelBindingContext bindingContext)
{
if (bindingContext == null)
{
throw new ArgumentNullException(nameof(bindingContext));
}
var modelName = bindingContext.ModelName;
var valueProviderResult = bindingContext.ValueProvider.GetValue(modelName);
if (valueProviderResult == ValueProviderResult.None)
{
return Task.CompletedTask;
}
bindingContext.ModelState.SetModelValue(modelName, valueProviderResult);
var dateValue = valueProviderResult.FirstValue;
if (string.IsNullOrEmpty(dateValue))
{
return Task.CompletedTask;
}
if (DateOnly.TryParseExact(dateValue, "dd.MM.yyyy", CultureInfo.InvariantCulture, DateTimeStyles.None, out var date))
{
bindingContext.Result = ModelBindingResult.Success(date);
}
else
{
bindingContext.ModelState.TryAddModelError(modelName, "Date must be in dd.MM.yyyy format.");
}
return Task.CompletedTask;
}
}
After creating the custom model binder, you need to inform ASP.NET Core to use this binder for DateOnly
types. You can do this by creating a model binder provider and adding it to the MVC options in your startup configuration.
public class DateOnlyModelBinderProvider : IModelBinderProvider
{
public IModelBinder GetBinder(ModelBinderProviderContext context)
{
if (context == null)
{
throw new ArgumentNullException(nameof(context));
}
if (context.Metadata.ModelType == typeof(DateOnly))
{
return new DateOnlyModelBinder();
}
return null;
}
}
In your dependency injection file you must use the binder provider like so:
public void ConfigureServices(IServiceCollection services)
{
services.AddControllers(options =>
{
// Add the custom model binder at the beginning of the collection
options.ModelBinderProviders.Insert(0, new DateOnlyModelBinderProvider());
});
}
By implementing a custom model binder like this, you explicitly control the date format handling and prevent common issues related to regional and cultural settings on different servers and clients.