最新版ABP 动态WebAPI 日期转json带T的解决方案| ABP DateTIme Json format
ABP动态webapi返回的json数据中,日期时间带T还有毫秒数的问题,在以往的版本中可以使用下面方法解决:
在XXXAbpWebApiModule中加上下面的代码:
很老的很老的版本有效:
public override void Initialize() {
Configuration.Modules.AbpWebApi().HttpConfiguration.Formatters.JsonFormatter.SerializerSettings.DateFormatString= "yyyy-MM-dd HH:mm:ss";
}
老版本有效:
public override void PostInitialize() {
Configuration.Modules.AbpWebApi().HttpConfiguration.Formatters.JsonFormatter.SerializerSettings.DateFormatString = "yyyy-MM-dd HH:mm:ss";
}
相对近期的版本有效,大概是v2.+:
好像是写到PostInitialize或者Initialize或者PreInitialize里面,具体可以逐一试试:
var converters = Configuration.Modules.AbpWebApi().HttpConfiguration.Formatters.JsonFormatter.SerializerSettings.Converters;
foreach (var converter in converters) {
if (converter is AbpDateTimeConverter) {
var tmpConverter = converter as AbpDateTimeConverter;
tmpConverter.DateTimeFormat = "yyyy-MM-dd HH:mm:ss";
}
}
下面是最近的版本,大概是v3.x有效:
public override void PostInitialize()
{
Configuration.Modules.AbpWebApi().HttpConfiguration.Formatters.JsonFormatter.SerializerSettings.ContractResolver = new MyAbpCamelCasePropertyNamesContractResolver();
}
public class MyAbpCamelCasePropertyNamesContractResolver : AbpCamelCasePropertyNamesContractResolver
{
protected override void ModifyProperty(MemberInfo member, JsonProperty property)
{
base.ModifyProperty(member, property);
if (property.Converter is AbpDateTimeConverter converter)
converter.DateTimeFormat = "yyyy-MM-dd HH:mm:ss";
}
}