1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950 |
- using System.Threading.Tasks;
- using Luticate2.Utils.Dbo.Result;
- using Microsoft.AspNetCore.Mvc.ModelBinding;
-
- namespace Luticate2.Utils.Dbo.Filter
- {
- public class LuFilterBinder : IModelBinder
- {
- public static LuResult<LuFilterDbo> FromString(string data)
- {
- if (data == null)
- {
- data = "";
- }
- data = data.Trim();
- var dbo = new LuFilterDbo
- {
- Query = data
- };
- return LuResult<LuFilterDbo>.Ok(dbo);
- }
-
- public Task BindModelAsync(ModelBindingContext bindingContext)
- {
- var messageTypeResult = bindingContext.ValueProvider.GetValue(bindingContext.ModelName);
- var res = FromString(messageTypeResult.FirstValue);
- if (res)
- {
- bindingContext.Result = ModelBindingResult.Success(res.Data);
- }
- else
- {
- bindingContext.Result = ModelBindingResult.Failed();
- }
- return Task.FromResult(0);
- }
- }
-
- public class LuFilterBinderProvider : IModelBinderProvider
- {
- public IModelBinder GetBinder(ModelBinderProviderContext context)
- {
- if (context.Metadata.ModelType == typeof(LuFilterDbo))
- {
- return new LuFilterBinder();
- }
- return null;
- }
- }
- }
|