1
This commit is contained in:
58
iMES.Core/ModelBinder/BaseBinderProvider.cs
Normal file
58
iMES.Core/ModelBinder/BaseBinderProvider.cs
Normal file
@@ -0,0 +1,58 @@
|
||||
using Microsoft.AspNetCore.Mvc.ModelBinding;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
|
||||
namespace iMES.Core.ModelBinder
|
||||
{
|
||||
public class BaseBinderProvider : IModelBinderProvider
|
||||
{
|
||||
public List<Type> types = new List<Type>();
|
||||
|
||||
// public List<BinderObject> binder=new BinderObject()
|
||||
public BaseBinderProvider()
|
||||
{
|
||||
types.Add(typeof(Dictionary<string, object>));
|
||||
InitType();
|
||||
}
|
||||
|
||||
private void AddType(Type type)
|
||||
{
|
||||
if (!types.Any(x => x == type))
|
||||
{
|
||||
types.Add(type);
|
||||
}
|
||||
}
|
||||
private void InitType()
|
||||
{
|
||||
AddType(typeof(Dictionary<string, object>));
|
||||
AddType(typeof(List<Dictionary<string, object>>));
|
||||
AddType(typeof(List<object>));
|
||||
AddType(typeof(List<iMES.Entity.DomainModels.Sys_TableColumn>));
|
||||
AddType(typeof(iMES.Entity.DomainModels.Sys_TableInfo));
|
||||
}
|
||||
/// <summary>
|
||||
/// 增加一个委托用于调用 return new BaseModelBinder();时对参数进行行校验,待完..
|
||||
/// </summary>
|
||||
/// <param name="types"></param>
|
||||
public BaseBinderProvider(List<Type> types)
|
||||
{
|
||||
this.types = types ?? throw new Exception("types初始化不能为null");
|
||||
InitType();
|
||||
}
|
||||
public IModelBinder GetBinder(ModelBinderProviderContext context)
|
||||
{
|
||||
if (context == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(context));
|
||||
}
|
||||
|
||||
if (types.Any(x => x == context.Metadata.ModelType))
|
||||
{
|
||||
return new BaseModelBinder();// new BinderTypeModelBinder(typeof(TableInfoEntityBinder));
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
46
iMES.Core/ModelBinder/BaseModelBinder.cs
Normal file
46
iMES.Core/ModelBinder/BaseModelBinder.cs
Normal file
@@ -0,0 +1,46 @@
|
||||
using Microsoft.AspNetCore.Mvc.ModelBinding;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
using iMES.Entity.DomainModels;
|
||||
|
||||
namespace iMES.Core.ModelBinder
|
||||
{
|
||||
public class BaseModelBinder : IModelBinder
|
||||
{
|
||||
public BaseModelBinder()
|
||||
{
|
||||
}
|
||||
|
||||
public Task BindModelAsync(ModelBindingContext bindingContext)
|
||||
{
|
||||
if (bindingContext == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(bindingContext));
|
||||
}
|
||||
|
||||
var modelName = bindingContext.ModelName;
|
||||
|
||||
// Try to fetch the value of the argument by name
|
||||
var valueProviderResult =
|
||||
bindingContext.ValueProvider.GetValue(modelName);
|
||||
|
||||
if (valueProviderResult == ValueProviderResult.None)
|
||||
{
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
bindingContext.ModelState.SetModelValue(modelName,
|
||||
valueProviderResult);
|
||||
|
||||
var value = valueProviderResult.FirstValue;
|
||||
if (string.IsNullOrEmpty(value))
|
||||
{
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
var model = Newtonsoft.Json.JsonConvert.DeserializeObject(value, bindingContext.ModelType);
|
||||
bindingContext.Result = ModelBindingResult.Success(model);
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
}
|
||||
}
|
||||
10
iMES.Core/ModelBinder/BinderObject.cs
Normal file
10
iMES.Core/ModelBinder/BinderObject.cs
Normal file
@@ -0,0 +1,10 @@
|
||||
using System;
|
||||
|
||||
namespace iMES.Core.ModelBinder
|
||||
{
|
||||
public class BinderObject<T> where T : class
|
||||
{
|
||||
public Type ModelType { get; set; }
|
||||
public Action<Func<T, object>> Filter { get; set; }
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user