|
|
|
@ -1,10 +1,11 @@
|
|
|
|
|
using Jint.Parser.Ast;
|
|
|
|
|
using Infrastructure.Resources;
|
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
|
using Microsoft.AspNetCore.Mvc.ModelBinding;
|
|
|
|
|
using Microsoft.AspNetCore.Mvc.ModelBinding.Metadata;
|
|
|
|
|
using Microsoft.AspNetCore.Mvc.Rendering;
|
|
|
|
|
using Microsoft.AspNetCore.Routing;
|
|
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
|
|
|
using Microsoft.Extensions.Localization;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.ComponentModel;
|
|
|
|
@ -63,6 +64,8 @@ namespace Infrastructure.Extensions
|
|
|
|
|
}
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
var factory = controller.HttpContext.RequestServices.GetService<IStringLocalizerFactory>();
|
|
|
|
|
var localizer = factory.Create("Resources.Resource", Assembly.GetEntryAssembly().GetName().Name);
|
|
|
|
|
dynamic json = new ExpandoObject();
|
|
|
|
|
var dictionary = (IDictionary<string, object>)json;
|
|
|
|
|
//type
|
|
|
|
@ -109,13 +112,13 @@ namespace Infrastructure.Extensions
|
|
|
|
|
{
|
|
|
|
|
json.title = metadata.DisplayName;
|
|
|
|
|
}
|
|
|
|
|
if (metadata.IsRequired)
|
|
|
|
|
{
|
|
|
|
|
if (metadata.ModelType != typeof(bool))
|
|
|
|
|
{
|
|
|
|
|
json.required = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
//if (metadata.IsRequired)
|
|
|
|
|
//{
|
|
|
|
|
// if (metadata.ModelType != typeof(bool))
|
|
|
|
|
// {
|
|
|
|
|
// json.required = true;
|
|
|
|
|
// }
|
|
|
|
|
//}
|
|
|
|
|
foreach (var attribute in metadata.Attributes.Attributes)
|
|
|
|
|
{
|
|
|
|
|
if (attribute is DescriptionAttribute descriptionAttribute)
|
|
|
|
@ -124,7 +127,16 @@ namespace Infrastructure.Extensions
|
|
|
|
|
}
|
|
|
|
|
else if (attribute is RequiredAttribute requiredAttribute)
|
|
|
|
|
{
|
|
|
|
|
json.required = requiredAttribute.ErrorMessage;
|
|
|
|
|
var localizedString = localizer.GetString(nameof(RequiredAttribute));
|
|
|
|
|
var message = requiredAttribute.ErrorMessage;
|
|
|
|
|
if (!localizedString.ResourceNotFound)
|
|
|
|
|
{
|
|
|
|
|
message = string.Format(localizedString.Value, metadata.DisplayName);
|
|
|
|
|
}
|
|
|
|
|
json.required = new
|
|
|
|
|
{
|
|
|
|
|
message = localizedString.ResourceNotFound ? requiredAttribute.FormatErrorMessage(metadata.DisplayName) : string.Format(localizedString.Value, metadata.DisplayName)
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
else if (attribute is RegularExpressionAttribute regularExpressionAttribute)
|
|
|
|
|
{//pattern
|
|
|
|
@ -212,23 +224,23 @@ namespace Infrastructure.Extensions
|
|
|
|
|
}
|
|
|
|
|
dynamic properties = new ExpandoObject();
|
|
|
|
|
var propertiesDictionary = (IDictionary<string, object>)properties;
|
|
|
|
|
var requiredList = new List<string>();
|
|
|
|
|
//var requiredList = new List<string>();
|
|
|
|
|
if (metadata.IsComplexType && !metadata.IsCollectionType && metadata.Properties.Any())
|
|
|
|
|
{
|
|
|
|
|
foreach (var item in metadata.Properties)
|
|
|
|
|
{
|
|
|
|
|
var modelMetadataItem = item as DefaultModelMetadata;
|
|
|
|
|
propertiesDictionary[item.PropertyName] = CreateJson(controller, modelMetadataItem);
|
|
|
|
|
if (modelMetadataItem.IsRequired)
|
|
|
|
|
{
|
|
|
|
|
requiredList.Add(item.PropertyName);
|
|
|
|
|
}
|
|
|
|
|
//if (modelMetadataItem.IsRequired)
|
|
|
|
|
//{
|
|
|
|
|
// requiredList.Add(item.PropertyName);
|
|
|
|
|
//}
|
|
|
|
|
}
|
|
|
|
|
json.properties = properties;
|
|
|
|
|
if (requiredList.Any())
|
|
|
|
|
{
|
|
|
|
|
json.required = requiredList;
|
|
|
|
|
}
|
|
|
|
|
// if (requiredList.Any())
|
|
|
|
|
// {
|
|
|
|
|
// json.required = requiredList;
|
|
|
|
|
// }
|
|
|
|
|
}
|
|
|
|
|
return json;
|
|
|
|
|
}
|
|
|
|
|