ASP.NET Core 1(RTM)中的自定义DateTime模型绑定程序 [英] Custom DateTime model binder in ASP.NET Core 1 (RTM)
问题描述
我正在编写ASP.NET Core 1应用程序,该应用程序具有Web API控制器,供供应商在其中发布数据.
I'm writing and ASP.NET Core 1 application, which has a Web API Controller where supplier will post data.
这是Model类的简化版本,我将绑定传入的数据:
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; }
}
问题在于日期将以德语格式发布,例如30.09.2016
.我不想将应用程序的全球文化设置为de-DE
,因为a)我不是德语,并且b)应用程序的其余部分都可以使用ISO日期.
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.
我已设置为编写自定义IModelBinder
,并且,因为它在ASP.NET Core MVC中显然是必需的,所以它是IModelBinderProvider
.
I've set to write a custom IModelBinder
and, because it's apparently obligatory in ASP.NET Core MVC, an IModelBinderProvider
.
这是我对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;
}
}
问题是我的IModelBinderProvider
仅在Lead
类(即context.Metadata.ModelType == typeof(Lead)
)上被击中.
The problem is that my IModelBinderProvider
is only being hit for the Lead
class, i.e., context.Metadata.ModelType == typeof(Lead)
.
我希望模型联编程序看到我们没有处理DateTime
,继续前进,然后然后返回Lead
类的每个属性.
显然不是这种情况,我的自定义IModelBinder
从未被点击.
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.
我说IModelBinderProvider
似乎是强制性的原因不是因为我没有找到直接将IModelBinder
注册到MVC管道中的方法.我已经注册了IModelBinderProvider:
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());
});
}
如何获取ASP.NET Core MVC将自定义的DateTime
绑定程序应用于Lead
类的Date
属性?
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
?
推荐答案
您是否将值发布为JSON? 如果是这样,我建议您向JSON.NET注册一个自定义JsonConverter. 在转换器中,您将使用具有不同DateFormatString的JsonSerializer,在这种情况下为德语日期格式. 所有其他JsonSerializer都不受此影响.
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);
}
这篇关于ASP.NET Core 1(RTM)中的自定义DateTime模型绑定程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!