DynamicController.cs 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217
  1. using Microsoft.AspNetCore.Builder;
  2. using Microsoft.AspNetCore.Mvc.ApplicationParts;
  3. using Microsoft.AspNetCore.Mvc;
  4. using Microsoft.Extensions.DependencyInjection;
  5. using Microsoft.Extensions.Options;
  6. using Microsoft.AspNetCore.Http;
  7. using System.Reflection;
  8. using Microsoft.AspNetCore.Mvc.ApplicationModels;
  9. using Microsoft.AspNetCore.Mvc.ActionConstraints;
  10. using Microsoft.AspNetCore.Mvc.ModelBinding;
  11. using Microsoft.AspNetCore.Mvc.Controllers;
  12. using System.Text.RegularExpressions;
  13. namespace EasyTemplate.Tool.Util;
  14. /// <summary>
  15. /// Add Dynamic WebApi
  16. /// </summary>
  17. public static class DynamicControllerRegister
  18. {
  19. /// <summary>
  20. /// Use Dynamic WebApi to Configure
  21. /// </summary>
  22. /// <param name="application"></param>
  23. /// <param name="options"></param>
  24. /// <returns></returns>
  25. public static IApplicationBuilder UseDynamicWebApi(this IApplicationBuilder application)
  26. {
  27. var options = new DynamicWebApiOptions();
  28. options.Valid();
  29. AppConsts.DefaultAreaName = options.DefaultAreaName;
  30. AppConsts.DefaultHttpVerb = options.DefaultHttpVerb;
  31. AppConsts.DefaultApiPreFix = options.DefaultApiPrefix;
  32. AppConsts.ControllerPostfixes = options.RemoveControllerPostfixes;
  33. AppConsts.ActionPostfixes = options.RemoveActionPostfixes;
  34. AppConsts.FormBodyBindingIgnoredTypes = options.FormBodyBindingIgnoredTypes;
  35. AppConsts.GetRestFulActionName = options.GetRestFulActionName;
  36. AppConsts.AssemblyDynamicWebApiOptions = options.AssemblyDynamicWebApiOptions;
  37. var partManager = application.ApplicationServices.GetRequiredService<ApplicationPartManager>();
  38. // Add a custom controller checker
  39. var featureProviders = application.ApplicationServices.GetRequiredService<DynamicWebApiControllerFeatureProvider>();
  40. partManager.FeatureProviders.Add(featureProviders);
  41. var mvcOptions = application.ApplicationServices.GetRequiredService<IOptions<MvcOptions>>();
  42. var dynamicWebApiConvention = application.ApplicationServices.GetRequiredService<DynamicWebApiConvention>();
  43. mvcOptions.Value.Conventions.Add(dynamicWebApiConvention);
  44. return application;
  45. }
  46. public static IServiceCollection AddDynamicController<TSelectController, TActionRouteFactory>(this IServiceCollection services)
  47. where TSelectController : class, ISelectController
  48. where TActionRouteFactory : class, IActionRouteFactory
  49. {
  50. services.AddSingleton<ISelectController, TSelectController>();
  51. services.AddSingleton<IActionRouteFactory, TActionRouteFactory>();
  52. services.AddSingleton<DynamicWebApiConvention>();
  53. services.AddSingleton<DynamicWebApiControllerFeatureProvider>();
  54. return services;
  55. }
  56. /// <summary>
  57. /// Add Dynamic WebApi to Container
  58. /// </summary>
  59. /// <param name="services"></param>
  60. /// <param name="options">configuration</param>
  61. /// <returns></returns>
  62. public static IServiceCollection AddDynamicController(this IServiceCollection services, DynamicWebApiOptions options)
  63. {
  64. if (options == null)
  65. {
  66. throw new ArgumentException(nameof(options));
  67. }
  68. options.Valid();
  69. AppConsts.DefaultAreaName = options.DefaultAreaName;
  70. AppConsts.DefaultHttpVerb = options.DefaultHttpVerb;
  71. AppConsts.DefaultApiPreFix = options.DefaultApiPrefix;
  72. AppConsts.ControllerPostfixes = options.RemoveControllerPostfixes;
  73. AppConsts.ActionPostfixes = options.RemoveActionPostfixes;
  74. AppConsts.FormBodyBindingIgnoredTypes = options.FormBodyBindingIgnoredTypes;
  75. AppConsts.GetRestFulActionName = options.GetRestFulActionName;
  76. AppConsts.AssemblyDynamicWebApiOptions = options.AssemblyDynamicWebApiOptions;
  77. var partManager = services.GetSingletonInstanceOrNull<ApplicationPartManager>();
  78. if (partManager == null)
  79. {
  80. throw new InvalidOperationException("\"AddDynamicWebApi\" must be after \"AddMvc\".");
  81. }
  82. // Add a custom controller checker
  83. partManager.FeatureProviders.Add(new DynamicWebApiControllerFeatureProvider(options.SelectController));
  84. services.Configure<MvcOptions>(o =>
  85. {
  86. // Register Controller Routing Information Converter
  87. o.Conventions.Add(new DynamicWebApiConvention(options.SelectController, options.ActionRouteFactory));
  88. });
  89. return services;
  90. }
  91. public static IServiceCollection AddDynamicWebApi(this IServiceCollection services)
  92. {
  93. return services.AddDynamicController(new DynamicWebApiOptions());
  94. }
  95. public static IServiceCollection AddDynamicWebApi(this IServiceCollection services, Action<DynamicWebApiOptions> optionsAction)
  96. {
  97. var dynamicWebApiOptions = new DynamicWebApiOptions();
  98. optionsAction?.Invoke(dynamicWebApiOptions);
  99. return services.AddDynamicController(dynamicWebApiOptions);
  100. }
  101. }
  102. public class DynamicWebApiOptions
  103. {
  104. public DynamicWebApiOptions()
  105. {
  106. RemoveControllerPostfixes = new List<string>() { "AppService", "ApplicationService" };
  107. RemoveActionPostfixes = new List<string>() { "Async" };
  108. FormBodyBindingIgnoredTypes = new List<Type>() { typeof(IFormFile) };
  109. DefaultHttpVerb = "POST";
  110. DefaultApiPrefix = "api";
  111. AssemblyDynamicWebApiOptions = new Dictionary<Assembly, AssemblyDynamicWebApiOptions>();
  112. }
  113. /// <summary>
  114. /// API HTTP Verb.
  115. /// <para></para>
  116. /// Default value is "POST".
  117. /// </summary>
  118. public string DefaultHttpVerb { get; set; }
  119. public string DefaultAreaName { get; set; }
  120. /// <summary>
  121. /// Routing prefix for all APIs
  122. /// <para></para>
  123. /// Default value is "api".
  124. /// </summary>
  125. public string DefaultApiPrefix { get; set; }
  126. /// <summary>
  127. /// Remove the dynamic API class(Controller) name postfix.
  128. /// <para></para>
  129. /// Default value is {"AppService", "ApplicationService"}.
  130. /// </summary>
  131. public List<string> RemoveControllerPostfixes { get; set; }
  132. /// <summary>
  133. /// Remove the dynamic API class's method(Action) postfix.
  134. /// <para></para>
  135. /// Default value is {"Async"}.
  136. /// </summary>
  137. public List<string> RemoveActionPostfixes { get; set; }
  138. /// <summary>
  139. /// Ignore MVC Form Binding types.
  140. /// </summary>
  141. public List<Type> FormBodyBindingIgnoredTypes { get; set; }
  142. /// <summary>
  143. /// The method that processing the name of the action.
  144. /// </summary>
  145. public Func<string, string> GetRestFulActionName { get; set; }
  146. /// <summary>
  147. /// Specifies the dynamic webapi options for the assembly.
  148. /// </summary>
  149. public Dictionary<Assembly, AssemblyDynamicWebApiOptions> AssemblyDynamicWebApiOptions { get; }
  150. public ISelectController SelectController { get; set; } = new DefaultSelectController();
  151. public IActionRouteFactory ActionRouteFactory { get; set; } = new DefaultActionRouteFactory();
  152. /// <summary>
  153. /// Verify that all configurations are valid
  154. /// </summary>
  155. public void Valid()
  156. {
  157. if (string.IsNullOrEmpty(DefaultHttpVerb))
  158. {
  159. throw new ArgumentException($"{nameof(DefaultHttpVerb)} can not be empty.");
  160. }
  161. if (string.IsNullOrEmpty(DefaultAreaName))
  162. {
  163. DefaultAreaName = string.Empty;
  164. }
  165. if (string.IsNullOrEmpty(DefaultApiPrefix))
  166. {
  167. DefaultApiPrefix = string.Empty;
  168. }
  169. if (FormBodyBindingIgnoredTypes == null)
  170. {
  171. throw new ArgumentException($"{nameof(FormBodyBindingIgnoredTypes)} can not be null.");
  172. }
  173. if (RemoveControllerPostfixes == null)
  174. {
  175. throw new ArgumentException($"{nameof(RemoveControllerPostfixes)} can not be null.");
  176. }
  177. }
  178. /// <summary>
  179. /// Add the dynamic webapi options for the assembly.
  180. /// </summary>
  181. /// <param name="assembly"></param>
  182. /// <param name="apiPreFix"></param>
  183. /// <param name="httpVerb"></param>
  184. public void AddAssemblyOptions(Assembly assembly, string apiPreFix = null, string httpVerb = null)
  185. {
  186. if (assembly == null)
  187. {
  188. throw new ArgumentException($"{nameof(assembly)} can not be null.");
  189. }
  190. AssemblyDynamicWebApiOptions[assembly] = new AssemblyDynamicWebApiOptions(apiPreFix, httpVerb);
  191. }
  192. }
  193. /// <summary>
  194. /// Specifies the dynamic webapi options for the assembly.
  195. /// </summary>
  196. public class AssemblyDynamicWebApiOptions
  197. {
  198. /// <summary>
  199. /// Routing prefix for all APIs
  200. /// <para></para>
  201. /// Default value is null.
  202. /// </summary>
  203. public string ApiPrefix { get; }
  204. /// <summary>
  205. /// API HTTP Verb.
  206. /// <para></para>
  207. /// Default value is null.
  208. /// </summary>
  209. public string HttpVerb { get; }
  210. /// <summary>
  211. ///
  212. /// </summary>
  213. /// <param name="apiPrefix">Routing prefix for all APIs</param>
  214. /// <param name="httpVerb">API HTTP Verb.</param>
  215. public AssemblyDynamicWebApiOptions(string apiPrefix = null, string httpVerb = null)
  216. {
  217. ApiPrefix = apiPrefix;
  218. HttpVerb = httpVerb;
  219. }
  220. }
  221. public interface ISelectController
  222. {
  223. bool IsController(Type type);
  224. }
  225. internal class DefaultSelectController : ISelectController
  226. {
  227. public bool IsController(Type type)
  228. {
  229. var typeInfo = type.GetTypeInfo();
  230. if (!typeof(IDynamicWebApi).IsAssignableFrom(type) ||
  231. !typeInfo.IsPublic || typeInfo.IsAbstract || typeInfo.IsGenericType)
  232. {
  233. return false;
  234. }
  235. var attr = ReflectionHelper.GetSingleAttributeOrDefaultByFullSearch<DynamicWebApiAttribute>(typeInfo);
  236. if (attr == null)
  237. {
  238. return false;
  239. }
  240. if (ReflectionHelper.GetSingleAttributeOrDefaultByFullSearch<NonDynamicWebApiAttribute>(typeInfo) != null)
  241. {
  242. return false;
  243. }
  244. return true;
  245. }
  246. }
  247. public interface IActionRouteFactory
  248. {
  249. string CreateActionRouteModel(string areaName, string controllerName, ActionModel action);
  250. }
  251. internal class DefaultActionRouteFactory : IActionRouteFactory
  252. {
  253. private static string GetApiPreFix(ActionModel action)
  254. {
  255. var getValueSuccess = AppConsts.AssemblyDynamicWebApiOptions
  256. .TryGetValue(action.Controller.ControllerType.Assembly, out AssemblyDynamicWebApiOptions assemblyDynamicWebApiOptions);
  257. if (getValueSuccess && !string.IsNullOrWhiteSpace(assemblyDynamicWebApiOptions?.ApiPrefix))
  258. {
  259. return assemblyDynamicWebApiOptions.ApiPrefix;
  260. }
  261. return AppConsts.DefaultApiPreFix;
  262. }
  263. public string CreateActionRouteModel(string areaName, string controllerName, ActionModel action)
  264. {
  265. var apiPreFix = GetApiPreFix(action);
  266. var routeStr = $"{apiPreFix}/{areaName}/{controllerName}/{action.ActionName}".Replace("//", "/");
  267. return routeStr;
  268. }
  269. }
  270. public class DynamicWebApiConvention : IApplicationModelConvention
  271. {
  272. private readonly ISelectController _selectController;
  273. private readonly IActionRouteFactory _actionRouteFactory;
  274. public DynamicWebApiConvention(ISelectController selectController, IActionRouteFactory actionRouteFactory)
  275. {
  276. _selectController = selectController;
  277. _actionRouteFactory = actionRouteFactory;
  278. }
  279. public void Apply(ApplicationModel application)
  280. {
  281. foreach (var controller in application.Controllers)
  282. {
  283. var type = controller.ControllerType.AsType();
  284. var dynamicWebApiAttr = ReflectionHelper.GetSingleAttributeOrDefaultByFullSearch<DynamicWebApiAttribute>(type.GetTypeInfo());
  285. if (!(_selectController is DefaultSelectController) && _selectController.IsController(type))
  286. {
  287. controller.ControllerName = controller.ControllerName.RemovePostFix(AppConsts.ControllerPostfixes.ToArray());
  288. ConfigureDynamicWebApi(controller, dynamicWebApiAttr);
  289. }
  290. else
  291. {
  292. if (typeof(IDynamicWebApi).GetTypeInfo().IsAssignableFrom(type))
  293. {
  294. controller.ControllerName = controller.ControllerName.RemovePostFix(AppConsts.ControllerPostfixes.ToArray());
  295. ConfigureArea(controller, dynamicWebApiAttr);
  296. ConfigureDynamicWebApi(controller, dynamicWebApiAttr);
  297. }
  298. else
  299. {
  300. if (dynamicWebApiAttr != null)
  301. {
  302. ConfigureArea(controller, dynamicWebApiAttr);
  303. ConfigureDynamicWebApi(controller, dynamicWebApiAttr);
  304. }
  305. }
  306. }
  307. }
  308. }
  309. private void ConfigureArea(ControllerModel controller, DynamicWebApiAttribute attr)
  310. {
  311. if (!controller.RouteValues.ContainsKey("area"))
  312. {
  313. if (attr == null)
  314. {
  315. throw new ArgumentException(nameof(attr));
  316. }
  317. if (!string.IsNullOrEmpty(attr.Module))
  318. {
  319. controller.RouteValues["area"] = attr.Module;
  320. }
  321. else if (!string.IsNullOrEmpty(AppConsts.DefaultAreaName))
  322. {
  323. controller.RouteValues["area"] = AppConsts.DefaultAreaName;
  324. }
  325. }
  326. }
  327. private void ConfigureDynamicWebApi(ControllerModel controller, DynamicWebApiAttribute controllerAttr)
  328. {
  329. ConfigureApiExplorer(controller);
  330. ConfigureSelector(controller, controllerAttr);
  331. ConfigureParameters(controller);
  332. }
  333. private void ConfigureParameters(ControllerModel controller)
  334. {
  335. foreach (var action in controller.Actions)
  336. {
  337. if (!CheckNoMapMethod(action))
  338. foreach (var para in action.Parameters)
  339. {
  340. if (para.BindingInfo != null)
  341. {
  342. continue;
  343. }
  344. if (!TypeHelper.IsPrimitiveExtendedIncludingNullable(para.ParameterInfo.ParameterType))
  345. {
  346. if (CanUseFormBodyBinding(action, para))
  347. {
  348. para.BindingInfo = BindingInfo.GetBindingInfo(new[] { new FromBodyAttribute() });
  349. }
  350. }
  351. }
  352. }
  353. }
  354. private bool CanUseFormBodyBinding(ActionModel action, ParameterModel parameter)
  355. {
  356. if (AppConsts.FormBodyBindingIgnoredTypes.Any(t => t.IsAssignableFrom(parameter.ParameterInfo.ParameterType)))
  357. {
  358. return false;
  359. }
  360. foreach (var selector in action.Selectors)
  361. {
  362. if (selector.ActionConstraints == null)
  363. {
  364. continue;
  365. }
  366. foreach (var actionConstraint in selector.ActionConstraints)
  367. {
  368. var httpMethodActionConstraint = actionConstraint as HttpMethodActionConstraint;
  369. if (httpMethodActionConstraint == null)
  370. {
  371. continue;
  372. }
  373. if (httpMethodActionConstraint.HttpMethods.All(hm => hm.IsIn("GET", "DELETE", "TRACE", "HEAD")))
  374. {
  375. return false;
  376. }
  377. }
  378. }
  379. return true;
  380. }
  381. #region ConfigureApiExplorer
  382. private void ConfigureApiExplorer(ControllerModel controller)
  383. {
  384. if (controller.ApiExplorer.GroupName.IsNullOrEmpty())
  385. {
  386. controller.ApiExplorer.GroupName = controller.ControllerName;
  387. }
  388. if (controller.ApiExplorer.IsVisible == null)
  389. {
  390. controller.ApiExplorer.IsVisible = true;
  391. }
  392. foreach (var action in controller.Actions)
  393. {
  394. if (!CheckNoMapMethod(action))
  395. ConfigureApiExplorer(action);
  396. }
  397. }
  398. private void ConfigureApiExplorer(ActionModel action)
  399. {
  400. if (action.ApiExplorer.IsVisible == null)
  401. {
  402. action.ApiExplorer.IsVisible = true;
  403. }
  404. }
  405. #endregion
  406. /// <summary>
  407. /// //不映射指定的方法
  408. /// </summary>
  409. /// <param name="action"></param>
  410. /// <returns></returns>
  411. private bool CheckNoMapMethod(ActionModel action)
  412. {
  413. bool isExist = false;
  414. var noMapMethod = ReflectionHelper.GetSingleAttributeOrDefault<NonDynamicMethodAttribute>(action.ActionMethod);
  415. if (noMapMethod != null)
  416. {
  417. action.ApiExplorer.IsVisible = false;//对应的Api不映射
  418. isExist = true;
  419. }
  420. return isExist;
  421. }
  422. private void ConfigureSelector(ControllerModel controller, DynamicWebApiAttribute controllerAttr)
  423. {
  424. if (controller.Selectors.Any(selector => selector.AttributeRouteModel != null))
  425. {
  426. return;
  427. }
  428. var areaName = string.Empty;
  429. if (controllerAttr != null)
  430. {
  431. areaName = controllerAttr.Module;
  432. }
  433. foreach (var action in controller.Actions)
  434. {
  435. if (!CheckNoMapMethod(action))
  436. ConfigureSelector(areaName, controller.ControllerName, action);
  437. }
  438. }
  439. private void ConfigureSelector(string areaName, string controllerName, ActionModel action)
  440. {
  441. var nonAttr = ReflectionHelper.GetSingleAttributeOrDefault<NonDynamicWebApiAttribute>(action.ActionMethod);
  442. if (nonAttr != null)
  443. {
  444. return;
  445. }
  446. if (action.Selectors.IsNullOrEmpty() || action.Selectors.Any(a => a.ActionConstraints.IsNullOrEmpty()))
  447. {
  448. if (!CheckNoMapMethod(action))
  449. AddAppServiceSelector(areaName, controllerName, action);
  450. }
  451. else
  452. {
  453. NormalizeSelectorRoutes(areaName, controllerName, action);
  454. }
  455. }
  456. private void AddAppServiceSelector(string areaName, string controllerName, ActionModel action)
  457. {
  458. var verb = GetHttpVerb(action);
  459. action.ActionName = GetRestFulActionName(action.ActionName);
  460. var appServiceSelectorModel = action.Selectors[0];
  461. if (appServiceSelectorModel.AttributeRouteModel == null)
  462. {
  463. appServiceSelectorModel.AttributeRouteModel = CreateActionRouteModel(areaName, controllerName, action);
  464. }
  465. if (!appServiceSelectorModel.ActionConstraints.Any())
  466. {
  467. appServiceSelectorModel.ActionConstraints.Add(new HttpMethodActionConstraint(new[] { verb }));
  468. switch (verb)
  469. {
  470. case "GET":
  471. appServiceSelectorModel.EndpointMetadata.Add(new HttpGetAttribute());
  472. break;
  473. case "POST":
  474. appServiceSelectorModel.EndpointMetadata.Add(new HttpPostAttribute());
  475. break;
  476. case "PUT":
  477. appServiceSelectorModel.EndpointMetadata.Add(new HttpPutAttribute());
  478. break;
  479. case "DELETE":
  480. appServiceSelectorModel.EndpointMetadata.Add(new HttpDeleteAttribute());
  481. break;
  482. default:
  483. throw new Exception($"Unsupported http verb: {verb}.");
  484. }
  485. }
  486. }
  487. /// <summary>
  488. /// Processing action name
  489. /// </summary>
  490. /// <param name="actionName"></param>
  491. /// <returns></returns>
  492. private static string GetRestFulActionName(string actionName)
  493. {
  494. // custom process action name
  495. var appConstsActionName = AppConsts.GetRestFulActionName?.Invoke(actionName);
  496. if (appConstsActionName != null)
  497. {
  498. return appConstsActionName;
  499. }
  500. // default process action name.
  501. // Remove Postfix
  502. //actionName = actionName.RemovePostFix(AppConsts.ActionPostfixes.ToArray());
  503. // Remove Prefix
  504. //var verbKey = actionName.GetPascalOrCamelCaseFirstWord().ToLower();
  505. //if (AppConsts.HttpVerbs.ContainsKey(verbKey))
  506. //{
  507. // if (actionName.Length == verbKey.Length)
  508. // {
  509. // return "";
  510. // }
  511. // else
  512. // {
  513. // return actionName.Substring(verbKey.Length);
  514. // }
  515. //}
  516. //else
  517. //{
  518. // return actionName;
  519. //}
  520. return actionName;
  521. }
  522. private void NormalizeSelectorRoutes(string areaName, string controllerName, ActionModel action)
  523. {
  524. action.ActionName = GetRestFulActionName(action.ActionName);
  525. foreach (var selector in action.Selectors)
  526. {
  527. //var aa = selector.EndpointMetadata.OfType<HttpMethodAttribute>().FirstOrDefault();
  528. //if (!string.IsNullOrWhiteSpace(aa.Template))
  529. //{
  530. // var res = aa.Template.ToLower().Replace("[controller]", controllerName.ToLower().Replace("service", ""));
  531. // res = res.Replace("[action]", action.DisplayName);
  532. // selector.EndpointMetadata.Remove(aa);
  533. // //selector.EndpointMetadata.Add(new HttpMethodAttribute(new[ "Get" ]));
  534. //}
  535. var bbbb = AttributeRouteModel.CombineAttributeRouteModel(CreateActionRouteModel(areaName, controllerName, action), selector.AttributeRouteModel);
  536. selector.AttributeRouteModel = selector.AttributeRouteModel is null ? CreateActionRouteModel(areaName, controllerName, action) : CreateActionRouteModel(areaName, controllerName, action, selector.AttributeRouteModel);
  537. }
  538. }
  539. private static string GetHttpVerb(ActionModel action)
  540. {
  541. var getValueSuccess = AppConsts.AssemblyDynamicWebApiOptions
  542. .TryGetValue(action.Controller.ControllerType.Assembly, out AssemblyDynamicWebApiOptions assemblyDynamicWebApiOptions);
  543. if (getValueSuccess && !string.IsNullOrWhiteSpace(assemblyDynamicWebApiOptions?.HttpVerb))
  544. {
  545. return assemblyDynamicWebApiOptions.HttpVerb;
  546. }
  547. var verbKey = action.ActionName.GetPascalOrCamelCaseFirstWord().ToLower();
  548. var verb = AppConsts.HttpVerbs.ContainsKey(verbKey) ? AppConsts.HttpVerbs[verbKey] : AppConsts.DefaultHttpVerb;
  549. return verb;
  550. }
  551. private AttributeRouteModel CreateActionRouteModel(string areaName, string controllerName, ActionModel action, AttributeRouteModel orgin = null)
  552. {
  553. if (orgin is null)
  554. {
  555. var route = _actionRouteFactory.CreateActionRouteModel(areaName, controllerName, action);
  556. return new AttributeRouteModel(new RouteAttribute(route));
  557. }
  558. else
  559. {
  560. controllerName = controllerName.Replace("Service", "");
  561. var route = orgin.Template.Replace("[Controller]", controllerName);
  562. route = route.Replace("[controller]", controllerName);
  563. route = route.Replace("[Action]", action.ActionName);
  564. route = route.Replace("[action]", action.ActionName);
  565. return new AttributeRouteModel(new RouteAttribute(route));
  566. }
  567. }
  568. }
  569. public static class AppConsts
  570. {
  571. public static string DefaultHttpVerb { get; set; }
  572. public static string DefaultAreaName { get; set; }
  573. public static string DefaultApiPreFix { get; set; }
  574. public static List<string> ControllerPostfixes { get; set; }
  575. public static List<string> ActionPostfixes { get; set; }
  576. public static List<Type> FormBodyBindingIgnoredTypes { get; set; }
  577. public static Dictionary<string, string> HttpVerbs { get; set; }
  578. public static Func<string, string> GetRestFulActionName { get; set; }
  579. public static Dictionary<Assembly, AssemblyDynamicWebApiOptions> AssemblyDynamicWebApiOptions { get; set; }
  580. static AppConsts()
  581. {
  582. HttpVerbs = new Dictionary<string, string>()
  583. {
  584. ["add"] = "POST",
  585. ["create"] = "POST",
  586. ["post"] = "POST",
  587. ["get"] = "GET",
  588. ["find"] = "GET",
  589. ["fetch"] = "GET",
  590. ["query"] = "GET",
  591. ["update"] = "PUT",
  592. ["put"] = "PUT",
  593. ["delete"] = "DELETE",
  594. ["remove"] = "DELETE",
  595. };
  596. }
  597. }
  598. public class DynamicWebApiControllerFeatureProvider : ControllerFeatureProvider
  599. {
  600. private ISelectController _selectController;
  601. public DynamicWebApiControllerFeatureProvider(ISelectController selectController)
  602. {
  603. _selectController = selectController;
  604. }
  605. protected override bool IsController(TypeInfo typeInfo)
  606. {
  607. return _selectController.IsController(typeInfo);
  608. }
  609. }
  610. internal static class ServiceCollectionExtensions
  611. {
  612. public static bool IsAdded<T>(this IServiceCollection services)
  613. {
  614. return services.IsAdded(typeof(T));
  615. }
  616. public static bool IsAdded(this IServiceCollection services, Type type)
  617. {
  618. return services.Any(d => d.ServiceType == type);
  619. }
  620. public static T GetSingletonInstanceOrNull<T>(this IServiceCollection services)
  621. {
  622. return (T)services
  623. .FirstOrDefault(d => d.ServiceType == typeof(T))
  624. ?.ImplementationInstance;
  625. }
  626. public static T GetSingletonInstance<T>(this IServiceCollection services)
  627. {
  628. var service = services.GetSingletonInstanceOrNull<T>();
  629. if (service == null)
  630. {
  631. throw new InvalidOperationException("Could not find singleton service: " + typeof(T).AssemblyQualifiedName);
  632. }
  633. return service;
  634. }
  635. public static IServiceProvider BuildServiceProviderFromFactory(this IServiceCollection services)
  636. {
  637. foreach (var service in services)
  638. {
  639. var factoryInterface = service.ImplementationInstance?.GetType()
  640. .GetTypeInfo()
  641. .GetInterfaces()
  642. .FirstOrDefault(i => i.GetTypeInfo().IsGenericType &&
  643. i.GetGenericTypeDefinition() == typeof(IServiceProviderFactory<>));
  644. if (factoryInterface == null)
  645. {
  646. continue;
  647. }
  648. var containerBuilderType = factoryInterface.GenericTypeArguments[0];
  649. return (IServiceProvider)typeof(ServiceCollectionExtensions)
  650. .GetTypeInfo()
  651. .GetMethods()
  652. .Single(m => m.Name == nameof(BuildServiceProviderFromFactory) && m.IsGenericMethod)
  653. .MakeGenericMethod(containerBuilderType)
  654. .Invoke(null, new object[] { services, null });
  655. }
  656. return services.BuildServiceProvider();
  657. }
  658. public static IServiceProvider BuildServiceProviderFromFactory<TContainerBuilder>(this IServiceCollection services, Action<TContainerBuilder> builderAction = null)
  659. {
  660. var serviceProviderFactory = services.GetSingletonInstanceOrNull<IServiceProviderFactory<TContainerBuilder>>();
  661. if (serviceProviderFactory == null)
  662. {
  663. throw new Exception($"Could not find {typeof(IServiceProviderFactory<TContainerBuilder>).FullName} in {services}.");
  664. }
  665. var builder = serviceProviderFactory.CreateBuilder(services);
  666. builderAction?.Invoke(builder);
  667. return serviceProviderFactory.CreateServiceProvider(builder);
  668. }
  669. }
  670. internal static class ReflectionHelper
  671. {
  672. public static TAttribute GetSingleAttributeOrDefaultByFullSearch<TAttribute>(TypeInfo info)
  673. where TAttribute : Attribute
  674. {
  675. var attributeType = typeof(TAttribute);
  676. if (info.IsDefined(attributeType, true))
  677. {
  678. return info.GetCustomAttributes(attributeType, true).Cast<TAttribute>().First();
  679. }
  680. else
  681. {
  682. foreach (var implInter in info.ImplementedInterfaces)
  683. {
  684. var res = GetSingleAttributeOrDefaultByFullSearch<TAttribute>(implInter.GetTypeInfo());
  685. if (res != null)
  686. {
  687. return res;
  688. }
  689. }
  690. }
  691. return null;
  692. }
  693. public static TAttribute GetSingleAttributeOrDefault<TAttribute>(MemberInfo memberInfo, TAttribute defaultValue = default, bool inherit = true)
  694. where TAttribute : Attribute
  695. {
  696. var attributeType = typeof(TAttribute);
  697. if (memberInfo.IsDefined(typeof(TAttribute), inherit))
  698. {
  699. return memberInfo.GetCustomAttributes(attributeType, inherit).Cast<TAttribute>().First();
  700. }
  701. return defaultValue;
  702. }
  703. /// <summary>
  704. /// Gets a single attribute for a member.
  705. /// </summary>
  706. /// <typeparam name="TAttribute">Type of the attribute</typeparam>
  707. /// <param name="memberInfo">The member that will be checked for the attribute</param>
  708. /// <param name="inherit">Include inherited attributes</param>
  709. /// <returns>Returns the attribute object if found. Returns null if not found.</returns>
  710. public static TAttribute GetSingleAttributeOrNull<TAttribute>(this MemberInfo memberInfo, bool inherit = true)
  711. where TAttribute : Attribute
  712. {
  713. if (memberInfo == null)
  714. {
  715. throw new ArgumentNullException(nameof(memberInfo));
  716. }
  717. var attrs = memberInfo.GetCustomAttributes(typeof(TAttribute), inherit).ToArray();
  718. if (attrs.Length > 0)
  719. {
  720. return (TAttribute)attrs[0];
  721. }
  722. return default;
  723. }
  724. public static TAttribute GetSingleAttributeOfTypeOrBaseTypesOrNull<TAttribute>(this Type type, bool inherit = true)
  725. where TAttribute : Attribute
  726. {
  727. var attr = type.GetTypeInfo().GetSingleAttributeOrNull<TAttribute>();
  728. if (attr != null)
  729. {
  730. return attr;
  731. }
  732. if (type.GetTypeInfo().BaseType == null)
  733. {
  734. return null;
  735. }
  736. return type.GetTypeInfo().BaseType.GetSingleAttributeOfTypeOrBaseTypesOrNull<TAttribute>(inherit);
  737. }
  738. }
  739. internal static class ExtensionMethods
  740. {
  741. public static bool IsNullOrEmpty(this string str)
  742. {
  743. return string.IsNullOrEmpty(str);
  744. }
  745. public static bool IsNullOrEmpty<T>(this ICollection<T> source)
  746. {
  747. return source == null || source.Count <= 0;
  748. }
  749. public static bool IsIn(this string str, params string[] data)
  750. {
  751. foreach (var item in data)
  752. {
  753. if (str == item)
  754. {
  755. return true;
  756. }
  757. }
  758. return false;
  759. }
  760. public static string RemovePostFix(this string str, params string[] postFixes)
  761. {
  762. if (str == null)
  763. {
  764. return null;
  765. }
  766. if (str == string.Empty)
  767. {
  768. return string.Empty;
  769. }
  770. if (postFixes.IsNullOrEmpty())
  771. {
  772. return str;
  773. }
  774. foreach (var postFix in postFixes)
  775. {
  776. if (str.EndsWith(postFix))
  777. {
  778. return str.Left(str.Length - postFix.Length);
  779. }
  780. }
  781. return str;
  782. }
  783. public static string RemovePreFix(this string str, params string[] preFixes)
  784. {
  785. if (str == null)
  786. {
  787. return null;
  788. }
  789. if (str == string.Empty)
  790. {
  791. return string.Empty;
  792. }
  793. if (preFixes.IsNullOrEmpty())
  794. {
  795. return str;
  796. }
  797. foreach (var preFix in preFixes)
  798. {
  799. if (str.StartsWith(preFix))
  800. {
  801. return str.Right(str.Length - preFix.Length);
  802. }
  803. }
  804. return str;
  805. }
  806. public static string Left(this string str, int len)
  807. {
  808. if (str == null)
  809. {
  810. throw new ArgumentNullException("str");
  811. }
  812. if (str.Length < len)
  813. {
  814. throw new ArgumentException("len argument can not be bigger than given string's length!");
  815. }
  816. return str.Substring(0, len);
  817. }
  818. public static string Right(this string str, int len)
  819. {
  820. if (str == null)
  821. {
  822. throw new ArgumentNullException("str");
  823. }
  824. if (str.Length < len)
  825. {
  826. throw new ArgumentException("len argument can not be bigger than given string's length!");
  827. }
  828. return str.Substring(str.Length - len, len);
  829. }
  830. public static string GetCamelCaseFirstWord(this string str)
  831. {
  832. if (str == null)
  833. {
  834. throw new ArgumentNullException(nameof(str));
  835. }
  836. if (str.Length == 1)
  837. {
  838. return str;
  839. }
  840. var res = Regex.Split(str, @"(?=\p{Lu}\p{Ll})|(?<=\p{Ll})(?=\p{Lu})");
  841. if (res.Length < 1)
  842. {
  843. return str;
  844. }
  845. else
  846. {
  847. return res[0];
  848. }
  849. }
  850. public static string GetPascalCaseFirstWord(this string str)
  851. {
  852. if (str == null)
  853. {
  854. throw new ArgumentNullException(nameof(str));
  855. }
  856. if (str.Length == 1)
  857. {
  858. return str;
  859. }
  860. var res = Regex.Split(str, @"(?=\p{Lu}\p{Ll})|(?<=\p{Ll})(?=\p{Lu})");
  861. if (res.Length < 2)
  862. {
  863. return str;
  864. }
  865. else
  866. {
  867. return res[1];
  868. }
  869. }
  870. public static string GetPascalOrCamelCaseFirstWord(this string str)
  871. {
  872. if (str == null)
  873. {
  874. throw new ArgumentNullException(nameof(str));
  875. }
  876. if (str.Length <= 1)
  877. {
  878. return str;
  879. }
  880. if (str[0] >= 65 && str[0] <= 90)
  881. {
  882. return str.GetPascalCaseFirstWord();
  883. }
  884. else
  885. {
  886. return str.GetCamelCaseFirstWord();
  887. }
  888. }
  889. }
  890. [Serializable]
  891. [AttributeUsage(AttributeTargets.Interface | AttributeTargets.Class)]
  892. public class DynamicWebApiAttribute : Attribute
  893. {
  894. /// <summary>
  895. /// Equivalent to AreaName
  896. /// </summary>
  897. public string Module { get; set; }
  898. internal static bool IsExplicitlyEnabledFor(Type type)
  899. {
  900. var remoteServiceAttr = type.GetTypeInfo().GetSingleAttributeOrNull<DynamicWebApiAttribute>();
  901. return remoteServiceAttr != null;
  902. }
  903. internal static bool IsExplicitlyDisabledFor(Type type)
  904. {
  905. var remoteServiceAttr = type.GetTypeInfo().GetSingleAttributeOrNull<DynamicWebApiAttribute>();
  906. return remoteServiceAttr != null;
  907. }
  908. internal static bool IsMetadataExplicitlyEnabledFor(Type type)
  909. {
  910. var remoteServiceAttr = type.GetTypeInfo().GetSingleAttributeOrNull<DynamicWebApiAttribute>();
  911. return remoteServiceAttr != null;
  912. }
  913. internal static bool IsMetadataExplicitlyDisabledFor(Type type)
  914. {
  915. var remoteServiceAttr = type.GetTypeInfo().GetSingleAttributeOrNull<DynamicWebApiAttribute>();
  916. return remoteServiceAttr != null;
  917. }
  918. internal static bool IsMetadataExplicitlyDisabledFor(MethodInfo method)
  919. {
  920. var remoteServiceAttr = method.GetSingleAttributeOrNull<DynamicWebApiAttribute>();
  921. return remoteServiceAttr != null;
  922. }
  923. internal static bool IsMetadataExplicitlyEnabledFor(MethodInfo method)
  924. {
  925. var remoteServiceAttr = method.GetSingleAttributeOrNull<DynamicWebApiAttribute>();
  926. return remoteServiceAttr != null;
  927. }
  928. }
  929. [Serializable]
  930. [AttributeUsage(AttributeTargets.Interface | AttributeTargets.Class | AttributeTargets.Method)]
  931. public class NonDynamicMethodAttribute : Attribute
  932. {
  933. }
  934. [Serializable]
  935. [AttributeUsage(AttributeTargets.Interface | AttributeTargets.Class | AttributeTargets.Method)]
  936. public class NonDynamicWebApiAttribute : Attribute
  937. {
  938. }
  939. public interface IDynamicWebApi
  940. {
  941. }
  942. internal static class TypeHelper
  943. {
  944. public static bool IsFunc(object obj)
  945. {
  946. if (obj == null)
  947. {
  948. return false;
  949. }
  950. var type = obj.GetType();
  951. if (!type.GetTypeInfo().IsGenericType)
  952. {
  953. return false;
  954. }
  955. return type.GetGenericTypeDefinition() == typeof(Func<>);
  956. }
  957. public static bool IsFunc<TReturn>(object obj)
  958. {
  959. return obj != null && obj.GetType() == typeof(Func<TReturn>);
  960. }
  961. public static bool IsPrimitiveExtendedIncludingNullable(Type type, bool includeEnums = false)
  962. {
  963. if (IsPrimitiveExtended(type, includeEnums))
  964. {
  965. return true;
  966. }
  967. if (type.GetTypeInfo().IsGenericType && type.GetGenericTypeDefinition() == typeof(Nullable<>))
  968. {
  969. return IsPrimitiveExtended(type.GenericTypeArguments[0], includeEnums);
  970. }
  971. return false;
  972. }
  973. private static bool IsPrimitiveExtended(Type type, bool includeEnums)
  974. {
  975. if (type.GetTypeInfo().IsPrimitive)
  976. {
  977. return true;
  978. }
  979. if (includeEnums && type.GetTypeInfo().IsEnum)
  980. {
  981. return true;
  982. }
  983. return type == typeof(string) ||
  984. type == typeof(decimal) ||
  985. type == typeof(DateTime) ||
  986. type == typeof(DateTimeOffset) ||
  987. type == typeof(TimeSpan) ||
  988. type == typeof(Guid);
  989. }
  990. }
  991. [AttributeUsage(AttributeTargets.Class)]
  992. public class DynamicControllerAttribute : Attribute
  993. {
  994. public DynamicControllerAttribute()
  995. {
  996. ServiceName = string.Empty;
  997. }
  998. public DynamicControllerAttribute(string serviceName)
  999. {
  1000. ServiceName = serviceName;
  1001. }
  1002. public string ServiceName { get; }
  1003. }