FromBodyOrDefaultAttribute.cs 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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 : ModelBinderAttribute
  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 FromBodyOrDefaultAttribute(BindType type, string fieldname, IConvertible defaultValue) : base(typeof(FromBodyOrDefaultModelBinder))
  49. {
  50. Type = type;
  51. FieldName = fieldname;
  52. DefaultValue = defaultValue;
  53. }
  54. public string FieldName { get; set; }
  55. public IConvertible DefaultValue { get; set; }
  56. /// <summary>
  57. /// 取值方式
  58. /// </summary>
  59. public BindType Type { get; set; }
  60. }
  61. }