To find all controllers and actions in an ASP.NET Core application using C#, you can use the IActionDescriptorCollectionProvider service. This service provides access to a collection of ActionDescriptor objects, which represent the actions in your application.
Here's an example of how to use the IActionDescriptorCollectionProvider service to find all controllers and actions in your application:
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc.Controllers; using Microsoft.AspNetCore.Mvc.Infrastructure; using Microsoft.Extensions.DependencyInjection; public class MyService { private readonly IActionDescriptorCollectionProvider _actionDescriptorCollectionProvider; public MyService(IActionDescriptorCollectionProvider actionDescriptorCollectionProvider) { _actionDescriptorCollectionProvider = actionDescriptorCollectionProvider; } public void FindAllControllersAndActions() { var actionDescriptors = _actionDescriptorCollectionProvider.ActionDescriptors.Items; foreach (var actionDescriptor in actionDescriptors) { if (actionDescriptor is ControllerActionDescriptor controllerActionDescriptor) { var controllerName = controllerActionDescriptor.ControllerName; var actionName = controllerActionDescriptor.ActionName; // Do something with the controller and action names // ... } } } } In this example, we inject the IActionDescriptorCollectionProvider service into our MyService class and use it to get the collection of ActionDescriptor objects using the ActionDescriptors.Items property. We then loop through each ActionDescriptor object in the collection and check if it is a ControllerActionDescriptor using the is keyword. If it is a ControllerActionDescriptor, we can get the controller and action names using the ControllerName and ActionName properties.
You can use the controllerName and actionName variables as needed in your application, for example, to generate URLs dynamically or to generate documentation for your API.
"C# find all controllers in an assembly"
var controllers = Assembly.GetExecutingAssembly().GetTypes().Where(type => typeof(Controller).IsAssignableFrom(type));
Controller class."Find all actions in a controller in C#"
var actions = typeof(MyController).GetMethods().Where(method => method.IsPublic && !method.IsSpecialName);
"C# find all controllers and actions in an assembly"
var controllersAndActions = Assembly.GetExecutingAssembly() .GetTypes() .Where(type => typeof(Controller).IsAssignableFrom(type)) .SelectMany(type => type.GetMethods() .Where(method => method.IsPublic && !method.IsSpecialName) .Select(method => new { Controller = type.Name, Action = method.Name })); "Find all controllers with a specific attribute in C#"
var controllersWithAttribute = Assembly.GetExecutingAssembly() .GetTypes() .Where(type => typeof(Controller).IsAssignableFrom(type) && type.GetCustomAttributes(typeof(MyAttribute), true).Any());
MyAttribute)."C# find all actions with a specific attribute in a controller"
var actionsWithAttribute = typeof(MyController).GetMethods() .Where(method => method.GetCustomAttributes(typeof(MyAttribute), true).Any());
MyAttribute)."Find all controllers with a specific naming convention in C#"
var controllersWithNamingConvention = Assembly.GetExecutingAssembly() .GetTypes() .Where(type => typeof(Controller).IsAssignableFrom(type) && type.Name.EndsWith("Controller")); "C# find all actions with specific HTTP method attribute in a controller"
var actionsWithHttpMethodAttribute = typeof(MyController).GetMethods() .Where(method => method.GetCustomAttributes(typeof(HttpPostAttribute), true).Any());
HttpPostAttribute in this example)."Find all controllers and actions in a specific namespace in C#"
var controllersAndActionsInNamespace = Assembly.GetExecutingAssembly() .GetTypes() .Where(type => typeof(Controller).IsAssignableFrom(type) && type.Namespace == "MyNamespace") .SelectMany(type => type.GetMethods() .Where(method => method.IsPublic && !method.IsSpecialName) .Select(method => new { Controller = type.Name, Action = method.Name })); "C# find all actions with specific return type in a controller"
var actionsWithReturnType = typeof(MyController).GetMethods() .Where(method => method.ReturnType == typeof(MyReturnType));
MyReturnType in this example)."Find all controllers and actions using reflection in C#"
var controllersAndActions = Assembly.GetExecutingAssembly() .GetTypes() .Where(type => typeof(Controller).IsAssignableFrom(type)) .SelectMany(type => type.GetMethods() .Where(method => method.IsPublic && !method.IsSpecialName) .Select(method => new { Controller = type.Name, Action = method.Name })); eager-loading mkmapview itoa portrait cronexpression typeahead kotlin-android-extensions mp3 tidytext stdin