添加项目文件。
This commit is contained in:
1 parent
05e131ca4e
commit
514d6f5255
1162 files changed
+81255
No files matched your search
@@ -0,0 +1,56 @@
|
||||
using System;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.AspNetCore.Mvc.ModelBinding;
|
||||
using WalkingTec.Mvvm.Core;
|
||||
|
||||
namespace WalkingTec.Mvvm.Mvc.Binders
|
||||
{
|
||||
/// <summary>
|
||||
/// DateRangeBinder
|
||||
/// DataRange model binding
|
||||
/// </summary>
|
||||
public class DateRangeBinder : IModelBinder
|
||||
{
|
||||
/// <summary>
|
||||
/// BindModelAsync
|
||||
/// </summary>
|
||||
/// <param name="bindingContext"></param>
|
||||
/// <returns></returns>
|
||||
public Task BindModelAsync(ModelBindingContext bindingContext)
|
||||
{
|
||||
if (bindingContext == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(bindingContext));
|
||||
}
|
||||
var modelName = bindingContext.ModelName;
|
||||
if (string.IsNullOrEmpty(modelName))
|
||||
{
|
||||
bindingContext.Result = ModelBindingResult.Success(null);
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
var valueProviderResult = bindingContext.ValueProvider.GetValue(modelName);
|
||||
if (valueProviderResult == ValueProviderResult.None)
|
||||
{
|
||||
modelName += "[Value]";
|
||||
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;
|
||||
}
|
||||
|
||||
if(DateRange.TryParse(value,out var dateRange))
|
||||
bindingContext.Result = ModelBindingResult.Success(dateRange);
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
using Microsoft.AspNetCore.Mvc.Controllers;
|
||||
using Microsoft.AspNetCore.Mvc.ModelBinding;
|
||||
using System;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace WalkingTec.Mvvm.Mvc.Binders
|
||||
{
|
||||
/// <summary>
|
||||
/// EnumBinder
|
||||
/// 忽略enum转换空字符串时的错误
|
||||
/// </summary>
|
||||
public class EnumBinder : IModelBinder
|
||||
{
|
||||
/// <summary>
|
||||
/// BindModelAsync
|
||||
/// </summary>
|
||||
/// <param name="bindingContext"></param>
|
||||
/// <returns></returns>
|
||||
public Task BindModelAsync(ModelBindingContext bindingContext)
|
||||
{
|
||||
if (bindingContext == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(bindingContext));
|
||||
}
|
||||
var modelName = bindingContext.ModelName;
|
||||
if (string.IsNullOrEmpty(modelName))
|
||||
{
|
||||
bindingContext.Result = ModelBindingResult.Success(null);
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
else
|
||||
{
|
||||
bindingContext.Result = ModelBindingResult.Success(Enum.Parse(bindingContext.ModelType,value));
|
||||
}
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
using Microsoft.AspNetCore.Mvc.ModelBinding;
|
||||
using Microsoft.AspNetCore.Mvc.ModelBinding.Binders;
|
||||
using System;
|
||||
using System.Collections;
|
||||
using WalkingTec.Mvvm.Core;
|
||||
|
||||
namespace WalkingTec.Mvvm.Mvc.Binders
|
||||
{
|
||||
/// <summary>
|
||||
/// CustomBinderProvider
|
||||
/// </summary>
|
||||
public class StringBinderProvider : IModelBinderProvider
|
||||
{
|
||||
/// <summary>
|
||||
/// GetBinder
|
||||
/// </summary>
|
||||
/// <param name="context"></param>
|
||||
/// <returns></returns>
|
||||
public IModelBinder GetBinder(ModelBinderProviderContext context)
|
||||
{
|
||||
if (context == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(context));
|
||||
}
|
||||
|
||||
if (context.Metadata.ModelType == typeof(DateRange))
|
||||
return new BinderTypeModelBinder(typeof(DateRangeBinder));
|
||||
|
||||
if (context.Metadata.ModelType == typeof(string))
|
||||
{
|
||||
return new BinderTypeModelBinder(typeof(StringIgnoreLTGTBinder));
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,61 @@
|
||||
using Microsoft.AspNetCore.Mvc.Controllers;
|
||||
using Microsoft.AspNetCore.Mvc.ModelBinding;
|
||||
using System;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using System.Web;
|
||||
|
||||
namespace WalkingTec.Mvvm.Mvc.Binders
|
||||
{
|
||||
/// <summary>
|
||||
/// StringIgnoreLTGTBinder
|
||||
/// 忽略客户端提交的 <及>字符
|
||||
/// </summary>
|
||||
public class StringIgnoreLTGTBinder : IModelBinder
|
||||
{
|
||||
/// <summary>
|
||||
/// BindModelAsync
|
||||
/// </summary>
|
||||
/// <param name="bindingContext"></param>
|
||||
/// <returns></returns>
|
||||
public Task BindModelAsync(ModelBindingContext bindingContext)
|
||||
{
|
||||
if (bindingContext == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(bindingContext));
|
||||
}
|
||||
var modelName = bindingContext.ModelName;
|
||||
if (string.IsNullOrEmpty(modelName))
|
||||
{
|
||||
bindingContext.Result = ModelBindingResult.Success(null);
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
var valueProviderResult =
|
||||
bindingContext.ValueProvider.GetValue(modelName);
|
||||
|
||||
if (valueProviderResult == ValueProviderResult.None)
|
||||
{
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
bindingContext.ModelState.SetModelValue(modelName, valueProviderResult);
|
||||
|
||||
var value = valueProviderResult.FirstValue;
|
||||
|
||||
var actDescriptor = bindingContext.ActionContext.ActionDescriptor as ControllerActionDescriptor;
|
||||
var count = actDescriptor
|
||||
.MethodInfo
|
||||
.CustomAttributes
|
||||
.Where(x => x.AttributeType == typeof(StringNeedLTGTAttribute)).Count();
|
||||
|
||||
if (count == 0)
|
||||
{
|
||||
value = HttpUtility.HtmlEncode(value);
|
||||
}
|
||||
|
||||
bindingContext.Result = ModelBindingResult.Success(value);
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
using System;
|
||||
|
||||
namespace WalkingTec.Mvvm.Mvc.Binders
|
||||
{
|
||||
/// <summary>
|
||||
/// StringNeedLTGT
|
||||
/// </summary>
|
||||
[AttributeUsage(AttributeTargets.Method, AllowMultiple = false)]
|
||||
public class StringNeedLTGTAttribute : Attribute
|
||||
{
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user