using System; using System.Collections.Generic; using System.Reflection; using Microsoft.AspNetCore.Http; using ZhonTai.DynamicApi.Enums; namespace ZhonTai.DynamicApi; public class DynamicApiOptions { public DynamicApiOptions() { RemoveControllerPostfixes = new List() { "AppService", "ApplicationService", "ApiController", "Controller", "Services", "Service" }; RemoveActionPostfixes = new List() { "Async" }; FormBodyBindingIgnoredTypes = new List() { typeof(IFormFile) }; DefaultHttpVerb = "POST"; DefaultApiPrefix = "api"; AssemblyDynamicApiOptions = new Dictionary(); } /// /// API HTTP Verb. /// /// Default value is "POST". /// public string DefaultHttpVerb { get; set; } public string DefaultAreaName { get; set; } /// /// Routing prefix for all APIs /// /// Default value is "api". /// public string DefaultApiPrefix { get; set; } /// /// Remove the dynamic API class(Controller) name postfix. /// /// Default value is {"AppService", "ApplicationService"}. /// public List RemoveControllerPostfixes { get; set; } /// /// Remove the dynamic API class's method(Action) postfix. /// /// Default value is {"Async"}. /// public List RemoveActionPostfixes { get; set; } /// /// Ignore MVC Form Binding types. /// public List FormBodyBindingIgnoredTypes { get; set; } /// /// Naming convention /// public NamingConventionEnum NamingConvention { get; set; } = NamingConventionEnum.KebabCase; /// /// The method that processing the name of the action. /// public Func GetRestFulActionName { get; set; } /// /// The method that processing the name of the controller. /// public Func GetRestFulControllerName { get; set; } /// /// Specifies the dynamic webapi options for the assembly. /// public Dictionary AssemblyDynamicApiOptions { get; } public ISelectController SelectController { get; set; } = new DefaultSelectController(); public IActionRouteFactory ActionRouteFactory { get; set; } = new DefaultActionRouteFactory(); public bool FormatResult { get; set; } = true; public Type FormatResultType { get; set; } = FormatResultContext.FormatResultType; /// /// Verify that all configurations are valid /// public void Valid() { if (string.IsNullOrEmpty(DefaultHttpVerb)) { throw new ArgumentException($"{nameof(DefaultHttpVerb)} can not be empty."); } if (string.IsNullOrEmpty(DefaultAreaName)) { DefaultAreaName = string.Empty; } if (string.IsNullOrEmpty(DefaultApiPrefix)) { DefaultApiPrefix = string.Empty; } if (FormBodyBindingIgnoredTypes == null) { throw new ArgumentException($"{nameof(FormBodyBindingIgnoredTypes)} can not be null."); } if (RemoveControllerPostfixes == null) { throw new ArgumentException($"{nameof(RemoveControllerPostfixes)} can not be null."); } } /// /// Add the dynamic webapi options for the assembly. /// /// /// /// public void AddAssemblyOptions(Assembly assembly, string apiPreFix = null, string httpVerb = null) { if (assembly == null) { throw new ArgumentException($"{nameof(assembly)} can not be null."); } this.AssemblyDynamicApiOptions[assembly] = new AssemblyDynamicApiOptions(apiPreFix, httpVerb); } /// /// Add the dynamic webapi options for the assemblies. /// /// /// /// public void AddAssemblyOptions(Assembly[] assemblies, string apiPreFix = null, string httpVerb = null) { if (assemblies == null) { throw new ArgumentException($"{nameof(assemblies)} can not be null."); } foreach (var assembly in assemblies) { AddAssemblyOptions(assembly, apiPreFix, httpVerb); } } }