-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathRouteRegistry.cs
30 lines (26 loc) · 905 Bytes
/
RouteRegistry.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
using System.Reflection;
namespace AspNetCoreFromScratch;
public class RouteRegistry
{
internal Dictionary<string, (Type Controller, MethodInfo Method)> Routes { get; } = new();
public RouteRegistry()
{
// Get all controllers
var controllers = Assembly.GetExecutingAssembly()
.GetTypes()
.Where(t => t.IsSubclassOf(typeof(ControllerBase)));
foreach (var controller in controllers)
{
var methods = controller.GetMethods();
foreach (var method in methods)
{
var routeAttr = method.GetCustomAttribute<RouteAttribute>();
if (routeAttr != null)
{
// Map the route to the controller and method that will be invoked
Routes.Add(routeAttr.Route, (controller, method));
}
}
}
}
}