0
0

DynamicApiOptions.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Reflection;
  4. using Microsoft.AspNetCore.Http;
  5. using ZhonTai.DynamicApi.Enums;
  6. namespace ZhonTai.DynamicApi;
  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. /// Naming convention
  49. /// </summary>
  50. public NamingConventionEnum NamingConvention { get; set; } = NamingConventionEnum.KebabCase;
  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. public bool FormatResult { get; set; } = true;
  66. public Type FormatResultType { get; set; } = FormatResultContext.FormatResultType;
  67. /// <summary>
  68. /// Verify that all configurations are valid
  69. /// </summary>
  70. public void Valid()
  71. {
  72. if (string.IsNullOrEmpty(DefaultHttpVerb))
  73. {
  74. throw new ArgumentException($"{nameof(DefaultHttpVerb)} can not be empty.");
  75. }
  76. if (string.IsNullOrEmpty(DefaultAreaName))
  77. {
  78. DefaultAreaName = string.Empty;
  79. }
  80. if (string.IsNullOrEmpty(DefaultApiPrefix))
  81. {
  82. DefaultApiPrefix = string.Empty;
  83. }
  84. if (FormBodyBindingIgnoredTypes == null)
  85. {
  86. throw new ArgumentException($"{nameof(FormBodyBindingIgnoredTypes)} can not be null.");
  87. }
  88. if (RemoveControllerPostfixes == null)
  89. {
  90. throw new ArgumentException($"{nameof(RemoveControllerPostfixes)} can not be null.");
  91. }
  92. }
  93. /// <summary>
  94. /// Add the dynamic webapi options for the assembly.
  95. /// </summary>
  96. /// <param name="assembly"></param>
  97. /// <param name="apiPreFix"></param>
  98. /// <param name="httpVerb"></param>
  99. public void AddAssemblyOptions(Assembly assembly, string apiPreFix = null, string httpVerb = null)
  100. {
  101. if (assembly == null)
  102. {
  103. throw new ArgumentException($"{nameof(assembly)} can not be null.");
  104. }
  105. this.AssemblyDynamicApiOptions[assembly] = new AssemblyDynamicApiOptions(apiPreFix, httpVerb);
  106. }
  107. /// <summary>
  108. /// Add the dynamic webapi options for the assemblies.
  109. /// </summary>
  110. /// <param name="assemblies"></param>
  111. /// <param name="apiPreFix"></param>
  112. /// <param name="httpVerb"></param>
  113. public void AddAssemblyOptions(Assembly[] assemblies, string apiPreFix = null, string httpVerb = null)
  114. {
  115. if (assemblies == null)
  116. {
  117. throw new ArgumentException($"{nameof(assemblies)} can not be null.");
  118. }
  119. foreach (var assembly in assemblies)
  120. {
  121. AddAssemblyOptions(assembly, apiPreFix, httpVerb);
  122. }
  123. }
  124. }