ControllerHelp.cs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Reflection;
  5. using System.Threading.Tasks;
  6. using Microsoft.AspNetCore.Mvc;
  7. namespace Sers.Serslot
  8. {
  9. public class ControllerHelp
  10. {
  11. #region Assembly_GetControllers
  12. public static IEnumerable<Type> Assembly_GetControllers(Assembly assembly)
  13. {
  14. return assembly.GetTypes().Where(type => typeof(Microsoft.AspNetCore.Mvc.ControllerBase).IsAssignableFrom(type) && !type.IsAbstract && type.IsPublic);
  15. }
  16. #endregion
  17. #region Controller_GetRoutePrefixs
  18. /// <summary>
  19. /// demo: ["/Auth/fold1/fold2","/api","/"]
  20. /// </summary>
  21. /// <param name="type"></param>
  22. /// <returns></returns>
  23. public static List<string> Controller_GetRoutePrefixs(Type type)
  24. {
  25. #region controllerName
  26. string controllerName = type.Name;
  27. if (controllerName.EndsWith("Controller"))
  28. {
  29. controllerName = controllerName.Substring(0, controllerName.Length - 10);
  30. }
  31. #endregion
  32. // [Route("api/[controller]")]
  33. var routePrefixs = type.GetCustomAttributes<Microsoft.AspNetCore.Mvc.RouteAttribute>().Select(routeAttribute =>
  34. {
  35. var routePrefix = routeAttribute.Template;
  36. if (routePrefix.StartsWith("/"))
  37. {
  38. routePrefix = routePrefix.Substring(1);
  39. }
  40. return routePrefix?.Replace("[controller]", controllerName);
  41. }).ToList();
  42. if (routePrefixs.Count == 0)
  43. {
  44. routePrefixs.Add("");
  45. }
  46. return routePrefixs;
  47. }
  48. #endregion
  49. #region Action_GetReturnType
  50. public static Type Action_GetReturnType(MethodInfo method)
  51. {
  52. var returnType = method.ReturnType;
  53. //Task
  54. if (returnType.IsGenericType && typeof(Task<>).IsAssignableFrom(returnType.GetGenericTypeDefinition()))
  55. {
  56. returnType = returnType.GetGenericArguments()[0];
  57. }
  58. //ActionResult
  59. if (returnType.IsGenericType && typeof(ActionResult<>).IsAssignableFrom(returnType.GetGenericTypeDefinition()))
  60. {
  61. returnType = returnType.GetGenericArguments()[0];
  62. }
  63. return returnType;
  64. }
  65. #endregion
  66. #region Action_GetRoutes
  67. /// <summary>
  68. ///
  69. /// </summary>
  70. /// <param name="routePrefixs">demo: ["/Auth/fold1/fold2","/api","/"]</param>
  71. /// <param name="method"></param>
  72. /// <returns></returns>
  73. public static List<(string route, string httpMethod, string oriRoute)> Action_GetRoutes(List<String> routePrefixs, MethodInfo method)
  74. {
  75. var routes = new List<(string route, string httpMethod, string oriRoute)>();
  76. var attrs = method.GetCustomAttributes<Microsoft.AspNetCore.Mvc.Routing.HttpMethodAttribute>();
  77. if (null == attrs || attrs.Count() == 0) return routes;
  78. var attrRoute = method.GetCustomAttribute<Microsoft.AspNetCore.Mvc.RouteAttribute>()?.Template ?? "";
  79. foreach (var attr in attrs)
  80. {
  81. var route = attr?.Template?.Trim() ?? attrRoute;
  82. if (route.StartsWith("/"))
  83. {
  84. // (x.1) 绝对路径。 /Station1/fold1/fold2/api1
  85. AddAbsRoute(route);
  86. }
  87. else
  88. {
  89. // (x.2) 相对路径。 fold1/fold2/api1
  90. routePrefixs.ForEach(routePrefix=>
  91. {
  92. string absRoute = "/" + routePrefix;
  93. if (!string.IsNullOrWhiteSpace(route))
  94. absRoute += "/" + route;
  95. AddAbsRoute(absRoute);
  96. });
  97. }
  98. #region Method AddAbsRoute
  99. void AddAbsRoute(string absRoute)
  100. {
  101. //absRoute 绝对路径。 /Station1/fold1/fold2/api1
  102. var oriRoute = absRoute;
  103. var Route = oriRoute;
  104. #region 处理特殊路由
  105. {
  106. //(x.x.1) /api/{action}/a
  107. Route = Route.Replace("[action]",method.Name);
  108. //(x.x.2) /api/Value/a?i=1
  109. int index = Route.IndexOf("?");
  110. if (index > 0)
  111. {
  112. Route = Route.Substring(0, index);
  113. }
  114. //(x.x.3) /api/Value/{id}
  115. index = Route.IndexOf('{');
  116. if (index > 0)
  117. {
  118. Route = Route.Substring(0, index);
  119. index = Route.LastIndexOf('/');
  120. Route = Route.Substring(0, index) + "/*";
  121. }
  122. }
  123. #endregion
  124. var HttpMethods = attr.HttpMethods;
  125. if (HttpMethods != null || HttpMethods.Count() > 0)
  126. {
  127. foreach (var httpMethod in HttpMethods)
  128. {
  129. routes.Add((Route, httpMethod,oriRoute));
  130. }
  131. }
  132. }
  133. #endregion
  134. }
  135. return routes;
  136. }
  137. #endregion
  138. }
  139. }