Blame view

netcore/src/Infrastructure/NCC/DynamicApiController/Internal/Penetrates.cs 3.25 KB
de2bd2f9   “wangming”   项目初始化
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
  using Microsoft.AspNetCore.Mvc;
  using System;
  using System.Collections.Concurrent;
  using System.Collections.Generic;
  using System.Reflection;
  
  namespace NCC.DynamicApiController
  {
      /// <summary>
      /// 常量、公共方法配置类
      /// </summary>
      internal static class Penetrates
      {
          /// <summary>
          /// 分组分隔符
          /// </summary>
          internal const string GroupSeparator = "@";
  
          /// <summary>
          /// 请求动词映射字典
          /// </summary>
          internal static ConcurrentDictionary<string, string> VerbToHttpMethods { get; private set; }
  
          /// <summary>
          /// 控制器排序集合
          /// </summary>
          internal static ConcurrentDictionary<string, int> ControllerOrderCollection { get; set; }
  
          /// <summary>
          /// 构造函数
          /// </summary>
          static Penetrates()
          {
              ControllerOrderCollection = new ConcurrentDictionary<string, int>();
  
              VerbToHttpMethods = new ConcurrentDictionary<string, string>
              {
                  ["post"] = "POST",
                  ["add"] = "POST",
                  ["create"] = "POST",
                  ["insert"] = "POST",
                  ["submit"] = "POST",
  
                  ["get"] = "GET",
                  ["find"] = "GET",
                  ["fetch"] = "GET",
                  ["query"] = "GET",
                  //["getlist"] = "GET",
                  //["getall"] = "GET",
  
                  ["put"] = "PUT",
                  ["update"] = "PUT",
  
                  ["delete"] = "DELETE",
                  ["remove"] = "DELETE",
                  ["clear"] = "DELETE",
  
                  ["patch"] = "PATCH"
              };
  
              IsApiControllerCached = new ConcurrentDictionary<Type, bool>();
          }
  
          /// <summary>
          /// <see cref="IsApiController(Type)"/> 缓存集合
          /// </summary>
          private static readonly ConcurrentDictionary<Type, bool> IsApiControllerCached;
  
          /// <summary>
          /// 是否是Api控制器
          /// </summary>
          /// <param name="type">type</param>
          /// <returns></returns>
          internal static bool IsApiController(Type type)
          {
              return IsApiControllerCached.GetOrAdd(type, Function);
  
              // 本地静态方法
              static bool Function(Type type)
              {
                  // 不能是非公开、基元类型、值类型、抽象类、接口、泛型类
                  if (!type.IsPublic || type.IsPrimitive || type.IsValueType || type.IsAbstract || type.IsInterface || type.IsGenericType) return false;
  
                  // 继承 ControllerBase  实现 IDynamicApiController 的类型  贴了 [DynamicApiController] 特性
                  if ((!typeof(Controller).IsAssignableFrom(type) && typeof(ControllerBase).IsAssignableFrom(type)) || typeof(IDynamicApiController).IsAssignableFrom(type) || type.IsDefined(typeof(DynamicApiControllerAttribute), true))
                  {
                      // 不是能被导出忽略的接口
                      if (type.IsDefined(typeof(ApiExplorerSettingsAttribute), true) && type.GetCustomAttribute<ApiExplorerSettingsAttribute>(true).IgnoreApi) return false;
  
                      return true;
                  }
                  return false;
              }
          }
      }
  }