I'm writing and ASP.NET Core 1 application, which has a Web API Controller where supplier will post data.
This is a simplified version of the Model class to which I will bind the incoming data:
public class Lead
{
public int SupplierId { get; set; }
public DateTime Date { get; set; }
}
The problem is that the dates will be posted with German formatting, for instance, 30.09.2016
. I do not want to set the global culture of the application to de-DE
because a) I'm not German, and b) the rest of the application will work with ISO dates.
I've set to write a custom IModelBinder
and, because it's apparently obligatory in ASP.NET Core MVC, an IModelBinderProvider
.
Here's my implementation of the IModelBinderProvider
:
public class GermanDateTimeModelBinderProvider : IModelBinderProvider
{
public IModelBinder GetBinder(ModelBinderProviderContext context)
{
if (context == null)
throw new ArgumentNullException(nameof(context));
if (!context.Metadata.IsComplexType &&
context.Metadata.ModelType == typeof(DateTime))
return new GermanDateTimeBinder();
return null;
}
}
The problem is that my IModelBinderProvider
is only being hit for the Lead
class, i.e., context.Metadata.ModelType == typeof(Lead)
.
I would expect the model binder to see that we're not dealing with a DateTime
, move on, and then come back for every property of the Lead
class.
This is apparently not the case, and my custom IModelBinder
never get hit.
The reason I say that IModelBinderProvider
appears to be obligatory no is that I've found no way of directly registering an IModelBinder
into the MVC pipeline; I have to register an IModelBinderProvider instead:
public void ConfigureServices(IServiceCollection services)
{
// Add framework services.
services.AddMvc(options =>
{
options.ModelBinderProviders.Insert(0, new GermanDateTimeModelBinderProvider());
});
}
How can I get ASP.NET Core MVC to apply my custom DateTime
binder to the Date
property of the Lead
class?
Is there maybe a way to skip the whole IModelBinderProvider
business and use just an IModelBinder
?
Do you post your values as JSON? If so, I'd suggest you register a custom JsonConverter with JSON.NET. In the converter you'll use a JsonSerializer with a different DateFormatString, in this case a German date format. All other JsonSerializer aren't affected by this.
public class Startup
{
public void ConfigureServices(IServiceCollection services)
{
services
.AddMvc()
.AddJsonOptions(option =>
{
option.SerializerSettings.Converters.Add(new LeadConverter());
});
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
loggerFactory.AddDebug();
app.UseMvcWithDefaultRoute();
}
}
public class LeadConverter : JsonConverter
{
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
throw new System.NotImplementedException();
}
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
var jsonSerializer = new JsonSerializer
{
DateFormatString = "dd.MM.yyyy"
};
return jsonSerializer.Deserialize<Lead>(reader);
}
public override bool CanConvert(Type objectType) => objectType == typeof(Lead);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With