Files
ThingsGateway/src/Admin/ThingsGateway.Furion/DynamicApiController/Internal/Penetrates.cs
2025-06-16 18:12:28 +08:00

119 lines
4.1 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// ------------------------------------------------------------------------
// 版权信息
// 版权归百小僧及百签科技(广东)有限公司所有。
// 所有权利保留。
// 官方网站https://baiqian.com
//
// 许可证信息
// 项目主要遵循 MIT 许可证和 Apache 许可证(版本 2.0)进行分发和使用。
// 许可证的完整文本可以在源代码树根目录中的 LICENSE-APACHE 和 LICENSE-MIT 文件中找到。
// ------------------------------------------------------------------------
using Microsoft.AspNetCore.Mvc;
using System.Collections.Concurrent;
namespace ThingsGateway.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, (string, int, Type)> ControllerOrderCollection { get; set; }
/// <summary>
/// 构造函数
/// </summary>
static Penetrates()
{
ControllerOrderCollection = new ConcurrentDictionary<string, (string, int, Type)>();
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);
return Function(type);
// 本地静态方法
static bool Function(Type type)
{
//// 排除 OData 控制器
//if (type.Assembly.GetName().Name.StartsWith("Microsoft.AspNetCore.OData")) return false;
// 不能是非公开、基元类型、值类型、抽象类、接口、泛型类
if (!type.IsPublic || type.IsPrimitive || type.IsValueType || type.IsAbstract || type.IsInterface || type.IsGenericType) return false;
// 如果控制器贴有 [NonController] 特性则忽略
if (type.IsDefined(typeof(NonControllerAttribute), false)) return false;
// 继承 ControllerBase 或 实现 IDynamicApiController 的类型 或 贴了 [DynamicApiController] 特性
if ((!typeof(Controller).IsAssignableFrom(type) && typeof(ControllerBase).IsAssignableFrom(type))
//|| typeof(IDynamicApiController).IsAssignableFrom(type)
//|| type.IsDefined(typeof(DynamicApiControllerAttribute), true)
// 支持没有继承 ControllerBase 且贴了 [Route] 特性的情况
|| (type.IsDefined(typeof(RouteAttribute), true)))
{
// 处理运行时动态生成程序集问题
//if (type.Assembly?.ManifestModule?.Name == "<Unknown>") return true;
// 解决 ASP.NET Core 启动时自动载入 NuGet 包导致模块化配置 SupportPackageNamePrefixs 出现非预期的结果
//if (!App.EffectiveTypes.Any(t => t == type)) return false;
return true;
}
return false;
}
}
}