ModelBindingContextExtension.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. using System.Net.Mime;
  2. using System.Text;
  3. using Microsoft.AspNetCore.Http;
  4. using Microsoft.AspNetCore.Mvc.Controllers;
  5. using Microsoft.AspNetCore.Mvc.ModelBinding;
  6. using Microsoft.Extensions.Primitives;
  7. using Newtonsoft.Json;
  8. namespace Masuit.Tools.AspNetCore.ModelBinder;
  9. internal static class ModelBindingContextExtension
  10. {
  11. /// <summary>
  12. /// 获取绑定参数对应的 Attribute
  13. /// </summary>
  14. /// <typeparam name="T">Attribute对象</typeparam>
  15. /// <param name="bindingContext">绑定参数上下文</param>
  16. /// <param name="parameterName">参数名称</param>
  17. /// <returns></returns>
  18. public static T GetAttribute<T>(this ModelBindingContext bindingContext, string parameterName = null) where T : Attribute
  19. {
  20. var fieldName = parameterName ?? bindingContext.FieldName;
  21. var ctrlActionDesc = bindingContext.ActionContext.ActionDescriptor as ControllerActionDescriptor;
  22. var fieldParameter = ctrlActionDesc!.MethodInfo.GetParameters().Single(p => p.Name == fieldName);
  23. return fieldParameter.GetCustomAttributes(typeof(T), false).Single() as T;
  24. }
  25. /// <summary>
  26. /// 获取请求体Body字符串内容
  27. /// </summary>
  28. /// <param name="context"></param>
  29. /// <param name="encoding"></param>
  30. /// <returns></returns>
  31. public static string GetBodyString(this HttpContext context, Encoding encoding)
  32. {
  33. context.Request.EnableBuffering(); //Ensure the HttpRequest.Body can be read multipletimes
  34. int contentLen = 255;
  35. if (context.Request.ContentLength != null)
  36. {
  37. contentLen = (int)context.Request.ContentLength;
  38. }
  39. var body = context.Request.Body;
  40. string bodyText;
  41. if (contentLen <= 0)
  42. {
  43. bodyText = "";
  44. }
  45. else
  46. {
  47. using var reader = new StreamReader(body, encoding, true, contentLen, true);
  48. bodyText = reader.ReadToEndAsync().Result;
  49. }
  50. body.Position = 0;
  51. return bodyText;
  52. }
  53. /// <summary>
  54. /// 转换为对应类型
  55. /// </summary>
  56. /// <param name="this"></param>
  57. /// <param name="type"></param>
  58. public static object ConvertObject(this object @this, Type type)
  59. {
  60. object value;
  61. if (@this is string str)
  62. {
  63. str = str.Trim();
  64. if ((str.StartsWith('[') && str.EndsWith(']')) || str.StartsWith('{') && str.EndsWith('}'))
  65. {
  66. value = JsonConvert.DeserializeObject(str, type);
  67. }
  68. else if ((str.StartsWith("\"[") && str.EndsWith("]\"")) || str.StartsWith("\"{") && str.EndsWith("}\""))
  69. {
  70. // json字符串 又被 json序列化 的情况
  71. var objects = JsonConvert.DeserializeObject(str);
  72. value = JsonConvert.SerializeObject(objects).ConvertObject(type);
  73. }
  74. else
  75. {
  76. var text = JsonConvert.SerializeObject(@this);
  77. value = JsonConvert.DeserializeObject(text, type);
  78. }
  79. }
  80. else if (@this is StringValues values)
  81. {
  82. var text = values.ToString();
  83. if (type.IsSimpleArrayType() || type.IsSimpleListType())
  84. {
  85. text = JsonConvert.SerializeObject(values);
  86. value = JsonConvert.DeserializeObject(text, type);
  87. }
  88. else
  89. {
  90. text = JsonConvert.SerializeObject(text);
  91. value = JsonConvert.DeserializeObject(text, type);
  92. }
  93. }
  94. else
  95. {
  96. var text = JsonConvert.SerializeObject(@this);
  97. value = JsonConvert.DeserializeObject(text, type);
  98. }
  99. return value;
  100. }
  101. }