روش یافتن لیست تمام کنترلرها و اکشن متدهای یک برنامهی ASP.NET Core
نویسنده: وحید نصیری
تاریخ: ۱۳۹۵/۱۰/۲۰ ۱۸:۵۵
آدرس: www.dntips.ir
| مطالب | ۳۶۹۴ |
| نویسندگان | ۲۷۶ |
| گروههای مطالب | ۱۰۲۴ |
| نقشههای راه | ۱۱۹ |
| دورهها | ۱۴ |
| اشتراکها | ۱۷۹۱۴ |
public class MvcActionViewModel
{
public string ControllerName { get; set; }
public string ActionName { get; set; }
public string AreaName { get; set; }
} public interface IMvcActionsDiscoveryService
{
ICollection<MvcActionViewModel> MvcActions { get; }
}
public class MvcActionsDiscoveryService : IMvcActionsDiscoveryService
{
public MvcActionsDiscoveryService(IActionDescriptorCollectionProvider actionDescriptorCollectionProvider)
{
var actionDescriptors = actionDescriptorCollectionProvider.ActionDescriptors.Items;
foreach (var actionDescriptor in actionDescriptors)
{
var descriptor = actionDescriptor as ControllerActionDescriptor;
if (descriptor == null)
{
continue;
}
var controllerTypeInfo = descriptor.ControllerTypeInfo;
var actionMethodInfo = descriptor.MethodInfo;
MvcActions.Add(new MvcActionViewModel
{
ControllerName = descriptor.ControllerName,
ActionName = descriptor.ActionName,
AreaName = controllerTypeInfo.GetCustomAttribute<AreaAttribute>()?.RouteValue
});
}
}
public ICollection<MvcActionViewModel> MvcActions { get; } = new HashSet<MvcActionViewModel>();
} public static class MvcActionsDiscoveryServiceExtensions
{
public static IServiceCollection AddMvcActionsDiscoveryService(this IServiceCollection services)
{
services.AddSingleton<IMvcActionsDiscoveryService, MvcActionsDiscoveryService>();
return services;
}
} public void ConfigureServices(IServiceCollection services)
{
services.AddMvcActionsDiscoveryService();
} PM> Install-Package DNTCommon.Web.Core
app.MapGet("/debug/routes", (IEnumerable<EndpointDataSource> endpointSources) =>
string.Join("\n", endpointSources.SelectMany(source => source.Endpoints)));