using System; using System.Collections.Generic; using System.Reflection; using System.Text; namespace Apewer.Web { /// API 入口。 [Serializable] public sealed class ApiEntry : IToJson { #region Reflection internal Assembly _assembly = null; internal Module _module = null; internal Type _type = null; internal MethodInfo _method = null; /// 定义当前入口的程序集。 public Assembly Assembly { get => _assembly; } /// 定义当前入口的模块。 public Module Module { get => _module; } /// 定义当前入口的类型。 public Type Type { get => _type; } /// 定义当前入口的方法。 public MethodInfo Method { get => _method; } #endregion #region Define internal CaptionAttribute _caption = null; internal HiddenAttribute _hidden = null; internal RoutePrefixAttribute _prefix = null; internal RouteAttribute _route = null; internal string _path = null; internal string _lower = null; /// 当前入口的路由前缀。 public RoutePrefixAttribute RoutePrefix { get => _prefix; } /// 当前入口的路由。 public RouteAttribute Route { get => _route; } /// 当前入口的标题。 public CaptionAttribute Caption { get => _caption; } /// 当前入口的路由路径。 public string Path { get => _path; } #endregion #region HTTP Method internal bool _restricted = false; internal HttpConnectAttribute _connect = null; internal HttpDeleteAttribute _delete = null; internal HttpGetAttribute _get = null; internal HttpHeadAttribute _head = null; internal HttpOptionsAttribute _options = null; internal HttpPatchAttribute _patch = null; internal HttpPostAttribute _post = null; internal HttpPutAttribute _put = null; internal HttpTraceAttribute _trace = null; /// 当前入口拥有 特性。 public HttpConnectAttribute Connect { get => _connect; } /// 当前入口拥有 特性。 public HttpDeleteAttribute Delete { get => _delete; } /// 当前入口拥有 特性。 public HttpGetAttribute Get { get => _get; } /// 当前入口拥有 特性。 public HttpHeadAttribute Head { get => _head; } /// 当前入口拥有 特性。 public HttpOptionsAttribute Options { get => _options; } /// 当前入口拥有 特性。 public HttpPatchAttribute Patch { get => _patch; } /// 当前入口拥有 特性。 public HttpPostAttribute Post { get => _post; } /// 当前入口拥有 特性。 public HttpPutAttribute Put { get => _put; } /// 当前入口拥有 特性。 public HttpTraceAttribute Trace { get => _trace; } #endregion private ApiEntry() { } internal static ApiEntry[] Parse(Type type) { return null; } internal static ApiEntry Parse(Type type, MethodInfo method, RoutePrefixAttribute prefix, bool typeIndependent) { if (type == null) return null; if (method == null) return null; if (!method.IsPublic) return null; if (method.IsGenericMethod) return null; if (method.IsStatic) return null; var route = RuntimeUtility.GetAttribute(method, false); var path = route == null ? null : Concat(prefix, route); var lower = path == null ? null : path.ToLower(); var entry = new ApiEntry(); entry._assembly = type.Assembly; entry._module = type.Module; entry._type = type; entry._method = method; entry._caption = RuntimeUtility.GetAttribute(method, true); entry._hidden = RuntimeUtility.GetAttribute(method, true); entry._prefix = prefix; entry._route = route; entry._path = path; entry._lower = lower; // 允许的 HTTP 方法,指定任何方法即表示需要限定方法。 entry._connect = RuntimeUtility.GetAttribute(method, false); entry._delete = RuntimeUtility.GetAttribute(method, false); entry._get = RuntimeUtility.GetAttribute(method, false); entry._head = RuntimeUtility.GetAttribute(method, false); entry._options = RuntimeUtility.GetAttribute(method, false); entry._patch = RuntimeUtility.GetAttribute(method, false); entry._post = RuntimeUtility.GetAttribute(method, false); entry._put = RuntimeUtility.GetAttribute(method, false); entry._trace = RuntimeUtility.GetAttribute(method, false); if (entry._get != null) entry._restricted = true; else if (entry._post != null) entry._restricted = true; else if (entry._options != null) entry._restricted = true; else if (entry._connect != null) entry._restricted = true; else if (entry._delete != null) entry._restricted = true; else if (entry._head != null) entry._restricted = true; else if (entry._patch != null) entry._restricted = true; else if (entry._put != null) entry._restricted = true; else if (entry._trace != null) entry._restricted = true; return entry; } static string Concat(RoutePrefixAttribute prefix, RouteAttribute route) { var segs = new List(16); if (prefix != null && !string.IsNullOrEmpty(prefix.Path)) { var split = prefix.Path.Split('/'); var count = split.Length; for (var i = 0; i < count; i++) { var seg = split[i]; if (string.IsNullOrEmpty(seg)) continue; segs.Add(seg); } } if (route != null && !string.IsNullOrEmpty(route.Path)) { var split = route.Path.Split('/'); var count = split.Length; for (var i = 0; i < count; i++) { var seg = split[i]; if (string.IsNullOrEmpty(seg)) continue; segs.Add(seg); } } if (segs.Count < 1) return "/"; return "/" + TextUtility.Join("/", segs.ToArray()); } /// 生成包含当前实例属性的 Json 对象。 /// Json 对象。 public Json ToJson() { var json = Json.NewObject(); return json; } } }