Example #1
0
    /// <inheritdoc />
    public virtual void RegisterAllRoutes(List <ChromelyController>?controllers)
    {
        if (controllers is null || !controllers.Any())
        {
            return;
        }

        try
        {
            foreach (var controller in controllers)
            {
                var controllerRoutesFactory = new ControllerRoutesFactory();
                ControllerRoutesFactory.CreateAndRegisterRoutes(this, controller, _routeParameterBinder, _dataTransferOptions);
            }
        }
        catch (Exception exception)
        {
            Logger.Instance.Log.LogError(exception);
        }
    }
    public static void CreateAndRegisterRoutes(IChromelyRouteProvider routeProvider, ChromelyController controller, IChromelyModelBinder routeParameterBinder, IChromelyDataTransferOptions dataTransferOptions)
    {
        if (routeProvider is null || controller is null)
        {
            return;
        }

        var methodInfos = controller.GetType().GetMethods()
                          .Where(m => m.GetCustomAttributes(typeof(ChromelyRouteAttribute), false).Length > 0)
                          .ToArray();

        foreach (var methodInfo in methodInfos)
        {
            var attribute = methodInfo.GetCustomAttribute <ChromelyRouteAttribute>();
            var key       = RouteKeys.CreateActionKey(controller.RoutePath, attribute?.Path ?? string.Empty);
            if (!routeProvider.RouteExists(key))
            {
                routeProvider.RegisterRoute(key, ControllerRoutesFactory.CreateDelegate(controller, methodInfo, routeParameterBinder, dataTransferOptions));
            }
        }
    }