PSTW_CentralizeSystem/Controllers/API/AdminAPI.cs
2024-11-25 16:31:24 +08:00

51 lines
2.1 KiB
C#

using Microsoft.AspNetCore.Mvc;
using PSTW_CentralSystem.DBContext;
using System.Reflection;
namespace PSTW_CentralSystem.Controllers.API
{
[ApiController]
[Route("[controller]")]
public class AdminAPI : Controller
{
private readonly ILogger<HomeController> _logger;
private readonly AuthDBContext _authDbContext;
public AdminAPI(ILogger<HomeController> logger, AuthDBContext authDbContext)
{
_logger = logger;
_authDbContext = authDbContext;
}
[HttpPost("GetClassAndMethodInformation")]
public async Task<IActionResult> GetClassAndMethodInformation(string moduleName)
{
var controllerAndMethodList = new object();
// Get the assembly containing the controllers
var assembly = Assembly.GetExecutingAssembly();
// Get all types in the assembly (controllers will typically be in the "Controllers" namespace)
//var controllerTypes = await Task.Run(() => assembly.GetTypes().Where(type => typeof(ControllerBase).IsAssignableFrom(type) && type.IsClass && type.Name.Contains("Controller") && type.Name != "AdminController") .ToList());
var controllerTypes = await Task.Run(() => assembly.GetTypes().Where(type => typeof(ControllerBase).IsAssignableFrom(type) && type.IsClass && type.Name.Contains(moduleName)).FirstOrDefault());
// Iterate over the controller types and get their methods
var methods = controllerTypes?.GetMethods(BindingFlags.Public | BindingFlags.Instance)
.Where(m => m.DeclaringType == controllerTypes) // Filter methods declared directly in the controller (ignoring inherited ones)
.Select(m => m.Name) // Get the method names
.ToList();
controllerAndMethodList = (new
{
Controller = controllerTypes?.Name,
Methods = methods
});
// Return the list as JSON
return Json(controllerAndMethodList);
}
}
}