mirror of https://github.com/abpframework/abp.git
3 changed files with 43 additions and 54 deletions
@ -1,48 +1,56 @@ |
|||||
using System; |
using System; |
||||
|
using System.Globalization; |
||||
using System.Linq; |
using System.Linq; |
||||
using Microsoft.AspNetCore.Mvc.ModelBinding; |
using Microsoft.AspNetCore.Mvc.ModelBinding; |
||||
|
using Microsoft.AspNetCore.Mvc.ModelBinding.Binders; |
||||
using Microsoft.AspNetCore.Mvc.ModelBinding.Metadata; |
using Microsoft.AspNetCore.Mvc.ModelBinding.Metadata; |
||||
|
using Microsoft.Extensions.DependencyInjection; |
||||
|
using Microsoft.Extensions.Logging; |
||||
using Volo.Abp.Timing; |
using Volo.Abp.Timing; |
||||
|
|
||||
namespace Volo.Abp.AspNetCore.Mvc.ModelBinding |
namespace Volo.Abp.AspNetCore.Mvc.ModelBinding |
||||
{ |
{ |
||||
|
|
||||
public class AbpDateTimeModelBinderProvider : IModelBinderProvider |
public class AbpDateTimeModelBinderProvider : IModelBinderProvider |
||||
{ |
{ |
||||
public IModelBinder GetBinder(ModelBinderProviderContext context) |
public IModelBinder GetBinder(ModelBinderProviderContext context) |
||||
{ |
{ |
||||
if (context.Metadata.ModelType != typeof(DateTime) && |
var modelType = context.Metadata.UnderlyingOrModelType; |
||||
context.Metadata.ModelType != typeof(DateTime?)) |
if (modelType == typeof(DateTime)) |
||||
{ |
|
||||
return null; |
|
||||
} |
|
||||
|
|
||||
if (context.Metadata.ContainerType == null) |
|
||||
{ |
{ |
||||
if (context.Metadata is DefaultModelMetadata defaultModelMetadata && |
if (context.Metadata.ContainerType == null) |
||||
defaultModelMetadata.Attributes.Attributes.All(x => x.GetType() != typeof(DisableDateTimeNormalizationAttribute))) |
|
||||
{ |
{ |
||||
return new AbpDateTimeModelBinder(context); |
if (context.Metadata is DefaultModelMetadata defaultModelMetadata && |
||||
|
defaultModelMetadata.Attributes.Attributes.All(x => x.GetType() != typeof(DisableDateTimeNormalizationAttribute))) |
||||
|
{ |
||||
|
return CreateAbpDateTimeModelBinder(context); |
||||
|
} |
||||
} |
} |
||||
} |
else |
||||
else |
{ |
||||
{ |
var dateNormalizationDisabledForClass = |
||||
var dateNormalizationDisabledForClass = |
context.Metadata.ContainerType.IsDefined(typeof(DisableDateTimeNormalizationAttribute), true); |
||||
context.Metadata.ContainerType.IsDefined(typeof(DisableDateTimeNormalizationAttribute), true); |
|
||||
|
|
||||
var dateNormalizationDisabledForProperty = context.Metadata.ContainerType |
var dateNormalizationDisabledForProperty = context.Metadata.ContainerType |
||||
.GetProperty(context.Metadata.PropertyName) |
.GetProperty(context.Metadata.PropertyName) |
||||
?.IsDefined(typeof(DisableDateTimeNormalizationAttribute), true); |
?.IsDefined(typeof(DisableDateTimeNormalizationAttribute), true); |
||||
|
|
||||
if (!dateNormalizationDisabledForClass && |
if (!dateNormalizationDisabledForClass && |
||||
dateNormalizationDisabledForProperty != null && |
dateNormalizationDisabledForProperty != null && |
||||
!dateNormalizationDisabledForProperty.Value) |
!dateNormalizationDisabledForProperty.Value) |
||||
{ |
{ |
||||
return new AbpDateTimeModelBinder(context); |
return CreateAbpDateTimeModelBinder(context); |
||||
|
} |
||||
} |
} |
||||
} |
} |
||||
|
|
||||
return null; |
return null; |
||||
} |
} |
||||
|
|
||||
|
protected virtual AbpDateTimeModelBinder CreateAbpDateTimeModelBinder(ModelBinderProviderContext context) |
||||
|
{ |
||||
|
const DateTimeStyles supportedStyles = DateTimeStyles.AllowWhiteSpaces | DateTimeStyles.AdjustToUniversal; |
||||
|
var dateTimeModelBinder = new DateTimeModelBinder(supportedStyles, context.Services.GetRequiredService<ILoggerFactory>()); |
||||
|
return new AbpDateTimeModelBinder(context.Services.GetRequiredService<IClock>(), dateTimeModelBinder); |
||||
|
} |
||||
} |
} |
||||
} |
} |
||||
|
|||||
Loading…
Reference in new issue