FromBodyOrDefaultAttribute.cs 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. using Microsoft.AspNetCore.Mvc;
  2. namespace Masuit.Tools.AspNetCore.ModelBinder
  3. {
  4. /// <summary>
  5. /// 自动装配声明参数值
  6. /// <list type="bullet">
  7. /// <item>
  8. /// 布尔
  9. /// <description>bool</description>
  10. /// </item>
  11. /// <item>
  12. /// 字符/字符串
  13. /// <description>char/string</description>
  14. /// </item>
  15. /// <item>
  16. /// 浮点型
  17. /// <description>float/double/decimal</description>
  18. /// </item>
  19. /// <item>
  20. /// 整型
  21. /// <description>byte/sbyte/short/ushort/int/uint/long/ulong</description>
  22. /// </item>
  23. /// <item>
  24. /// 枚举
  25. /// <description>Enum</description>
  26. /// </item>
  27. /// <item>
  28. /// 其他类型
  29. /// <description>JObject/XDocument/Uri/Guid/TimeSpan</description>
  30. /// </item>
  31. /// </list>
  32. /// </summary>
  33. [AttributeUsage(AttributeTargets.Parameter)]
  34. public class FromBodyOrDefaultAttribute(BindType type, string fieldname, IConvertible defaultValue) : ModelBinderAttribute(typeof(FromBodyOrDefaultModelBinder))
  35. {
  36. public FromBodyOrDefaultAttribute() : this(BindType.Default, null, null)
  37. {
  38. }
  39. public FromBodyOrDefaultAttribute(IConvertible defaultValue) : this(BindType.Default, null, defaultValue)
  40. {
  41. }
  42. public FromBodyOrDefaultAttribute(BindType type) : this(type, null, null)
  43. {
  44. }
  45. public FromBodyOrDefaultAttribute(BindType type, string fieldname) : this(type, fieldname, null)
  46. {
  47. }
  48. public string FieldName { get; set; } = fieldname;
  49. public IConvertible DefaultValue { get; set; } = defaultValue;
  50. /// <summary>
  51. /// 取值方式
  52. /// </summary>
  53. public BindType Type { get; set; } = type;
  54. }
  55. }