You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

LuFilterBinder.cs 1.4KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. using System.Threading.Tasks;
  2. using Luticate2.Utils.Dbo.Result;
  3. using Microsoft.AspNetCore.Mvc.ModelBinding;
  4. namespace Luticate2.Utils.Dbo.Filter
  5. {
  6. public class LuFilterBinder : IModelBinder
  7. {
  8. public static LuResult<LuFilterDbo> FromString(string data)
  9. {
  10. if (data == null)
  11. {
  12. data = "";
  13. }
  14. data = data.Trim();
  15. var dbo = new LuFilterDbo
  16. {
  17. Query = data
  18. };
  19. return LuResult<LuFilterDbo>.Ok(dbo);
  20. }
  21. public Task BindModelAsync(ModelBindingContext bindingContext)
  22. {
  23. var messageTypeResult = bindingContext.ValueProvider.GetValue(bindingContext.ModelName);
  24. var res = FromString(messageTypeResult.FirstValue);
  25. if (res)
  26. {
  27. bindingContext.Result = ModelBindingResult.Success(res.Data);
  28. }
  29. else
  30. {
  31. bindingContext.Result = ModelBindingResult.Failed();
  32. }
  33. return Task.FromResult(0);
  34. }
  35. }
  36. public class LuFilterBinderProvider : IModelBinderProvider
  37. {
  38. public IModelBinder GetBinder(ModelBinderProviderContext context)
  39. {
  40. if (context.Metadata.ModelType == typeof(LuFilterDbo))
  41. {
  42. return new LuFilterBinder();
  43. }
  44. return null;
  45. }
  46. }
  47. }