ApiGroupConvention.cs 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. using Microsoft.AspNetCore.Mvc;
  2. using Microsoft.AspNetCore.Mvc.ApplicationModels;
  3. using ZhonTai.DynamicApi.Attributes;
  4. namespace ZhonTai.Admin.Core.Conventions;
  5. /// <summary>
  6. /// Api分组约定
  7. /// </summary>
  8. public class ApiGroupConvention : IControllerModelConvention
  9. {
  10. public void Apply(ControllerModel controller)
  11. {
  12. if (controller.Attributes?.Count > 0)
  13. {
  14. foreach (var attribute in controller.Attributes)
  15. {
  16. if (attribute is AreaAttribute area)
  17. {
  18. if (controller.ApiExplorer.GroupName.IsNull())
  19. {
  20. controller.ApiExplorer.GroupName = area.RouteValue?.ToLower();
  21. }
  22. break;
  23. }
  24. else if (attribute is DynamicApiAttribute dynamicApi)
  25. {
  26. if (controller.ApiExplorer.GroupName.IsNull())
  27. {
  28. controller.ApiExplorer.GroupName = dynamicApi.Area?.ToLower();
  29. }
  30. break;
  31. }
  32. }
  33. }
  34. }
  35. }