ApiFeatureProvider.cs 1.2 KB

12345678910111213141516171819202122232425262728
  1. using System;
  2. using System.Reflection;
  3. using Microsoft.AspNetCore.Mvc;
  4. using Microsoft.AspNetCore.Mvc.Controllers;
  5. namespace ZR.Common.DynamicApiSimple;
  6. class ApiFeatureProvider : ControllerFeatureProvider
  7. {
  8. protected override bool IsController(TypeInfo typeInfo)
  9. {
  10. Type type = typeInfo.AsType();
  11. // 不能是非公开的、值类型、抽象类、泛型类或基元类型
  12. if (!type.IsPublic || type.IsValueType || type.IsAbstract || type.IsGenericType || type.IsPrimitive || string.IsNullOrWhiteSpace(type.Namespace)) return false;
  13. // 原生层或者实现IDynamicApiController(类),[DynamicApi](接口)
  14. if ((!typeof(Controller).IsAssignableFrom(type) && typeof(ControllerBase).IsAssignableFrom(type)) || type.IsDefined(typeof(DynamicApiAttribute), true) || typeof(IDynamicApi).IsAssignableFrom(type))
  15. {
  16. // 如果是忽略的则跳过自定义的接口在前面会报错,所以必须在后面
  17. if (type.IsDefined(typeof(ApiExplorerSettingsAttribute), true) && type.GetCustomAttribute<ApiExplorerSettingsAttribute>(true).IgnoreApi)
  18. {
  19. return false;
  20. }
  21. return true;
  22. }
  23. return false;
  24. }
  25. }