DynamicApiOptions.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Reflection;
  4. using Microsoft.AspNetCore.Http;
  5. namespace ZhonTai.Tools.DynamicApi
  6. {
  7. public class DynamicApiOptions
  8. {
  9. public DynamicApiOptions()
  10. {
  11. RemoveControllerPostfixes = new List<string>() { "AppService", "ApplicationService", "ApiController", "Controller", "Services", "Service" };
  12. RemoveActionPostfixes = new List<string>() { "Async" };
  13. FormBodyBindingIgnoredTypes = new List<Type>() { typeof(IFormFile) };
  14. DefaultHttpVerb = "POST";
  15. DefaultApiPrefix = "api";
  16. AssemblyDynamicApiOptions = new Dictionary<Assembly, AssemblyDynamicApiOptions>();
  17. }
  18. /// <summary>
  19. /// API HTTP Verb.
  20. /// <para></para>
  21. /// Default value is "POST".
  22. /// </summary>
  23. public string DefaultHttpVerb { get; set; }
  24. public string DefaultAreaName { get; set; }
  25. /// <summary>
  26. /// Routing prefix for all APIs
  27. /// <para></para>
  28. /// Default value is "api".
  29. /// </summary>
  30. public string DefaultApiPrefix { get; set; }
  31. /// <summary>
  32. /// Remove the dynamic API class(Controller) name postfix.
  33. /// <para></para>
  34. /// Default value is {"AppService", "ApplicationService"}.
  35. /// </summary>
  36. public List<string> RemoveControllerPostfixes { get; set; }
  37. /// <summary>
  38. /// Remove the dynamic API class's method(Action) postfix.
  39. /// <para></para>
  40. /// Default value is {"Async"}.
  41. /// </summary>
  42. public List<string> RemoveActionPostfixes { get; set; }
  43. /// <summary>
  44. /// Ignore MVC Form Binding types.
  45. /// </summary>
  46. public List<Type> FormBodyBindingIgnoredTypes { get; set; }
  47. /// <summary>
  48. /// Both the controller name and the action name are pascal to kebabCase
  49. /// </summary>
  50. public bool PascalToKebabCase { get; set; } = true;
  51. /// <summary>
  52. /// The method that processing the name of the action.
  53. /// </summary>
  54. public Func<string, string> GetRestFulActionName { get; set; }
  55. /// <summary>
  56. /// The method that processing the name of the controller.
  57. /// </summary>
  58. public Func<string, string> GetRestFulControllerName { get; set; }
  59. /// <summary>
  60. /// Specifies the dynamic webapi options for the assembly.
  61. /// </summary>
  62. public Dictionary<Assembly, AssemblyDynamicApiOptions> AssemblyDynamicApiOptions { get; }
  63. public ISelectController SelectController { get; set; } = new DefaultSelectController();
  64. public IActionRouteFactory ActionRouteFactory { get; set; } = new DefaultActionRouteFactory();
  65. /// <summary>
  66. /// Verify that all configurations are valid
  67. /// </summary>
  68. public void Valid()
  69. {
  70. if (string.IsNullOrEmpty(DefaultHttpVerb))
  71. {
  72. throw new ArgumentException($"{nameof(DefaultHttpVerb)} can not be empty.");
  73. }
  74. if (string.IsNullOrEmpty(DefaultAreaName))
  75. {
  76. DefaultAreaName = string.Empty;
  77. }
  78. if (string.IsNullOrEmpty(DefaultApiPrefix))
  79. {
  80. DefaultApiPrefix = string.Empty;
  81. }
  82. if (FormBodyBindingIgnoredTypes == null)
  83. {
  84. throw new ArgumentException($"{nameof(FormBodyBindingIgnoredTypes)} can not be null.");
  85. }
  86. if (RemoveControllerPostfixes == null)
  87. {
  88. throw new ArgumentException($"{nameof(RemoveControllerPostfixes)} can not be null.");
  89. }
  90. }
  91. /// <summary>
  92. /// Add the dynamic webapi options for the assembly.
  93. /// </summary>
  94. /// <param name="assembly"></param>
  95. /// <param name="apiPreFix"></param>
  96. /// <param name="httpVerb"></param>
  97. public void AddAssemblyOptions(Assembly assembly, string apiPreFix = null, string httpVerb = null)
  98. {
  99. if (assembly == null)
  100. {
  101. throw new ArgumentException($"{nameof(assembly)} can not be null.");
  102. }
  103. this.AssemblyDynamicApiOptions[assembly] = new AssemblyDynamicApiOptions(apiPreFix, httpVerb);
  104. }
  105. /// <summary>
  106. /// Add the dynamic webapi options for the assemblies.
  107. /// </summary>
  108. /// <param name="assemblies"></param>
  109. /// <param name="apiPreFix"></param>
  110. /// <param name="httpVerb"></param>
  111. public void AddAssemblyOptions(Assembly[] assemblies, string apiPreFix = null, string httpVerb = null)
  112. {
  113. if (assemblies == null)
  114. {
  115. throw new ArgumentException($"{nameof(assemblies)} can not be null.");
  116. }
  117. foreach (var assembly in assemblies)
  118. {
  119. AddAssemblyOptions(assembly, apiPreFix, httpVerb);
  120. }
  121. }
  122. }
  123. }