Skip to content

Instantly share code, notes, and snippets.

@ritasker
Last active September 24, 2022 19:08
Show Gist options
  • Save ritasker/fc5cfbfb82bcf28534d3ac1c3bd46ea7 to your computer and use it in GitHub Desktop.
Save ritasker/fc5cfbfb82bcf28534d3ac1c3bd46ea7 to your computer and use it in GitHub Desktop.
public class NodaTimeModelBinder : ModelBinderBase
{
private readonly JsonSerializerOptions serializerOptions;
public NodaTimeModelBinder(ILoggerFactory loggerFactory) : base(loggerFactory)
{
serializerOptions = new JsonSerializerOptions {PropertyNameCaseInsensitive = true};
serializerOptions.ConfigureForNodaTime(DateTimeZoneProviders.Tzdb);
}
protected override async Task<T> BindCore<T>(HttpRequest request)
{
return await JsonSerializer.DeserializeAsync<T>(request.Body, serializerOptions);
}
}
public class Startup
{
public void ConfigureServices(IServiceCollection services)
{
services.AddCarter(configurator: c =>
{
c.WithModelBinder<NodaTimeModelBinder>();
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment