private static void AddNewApiRoutes(IServiceRoutes routes, Assembly assembly) { var services = assembly.GetExportedTypes() .Where(t => !t.IsAbstract && t.HasInterface(typeof(IService))); foreach (Type service in services) { var allServiceActions = service.GetActions(); foreach (var requestDtoActions in allServiceActions.GroupBy(x => x.GetParameters()[0].ParameterType)) { var requestType = requestDtoActions.Key; var hasWildcard = requestDtoActions.Any(x => x.Name.EqualsIgnoreCase(ActionContext.AnyAction)); string allowedVerbs = null; //null == All Routes if (!hasWildcard) { var allowedMethods = new List <string>(); foreach (var action in requestDtoActions) { allowedMethods.Add(action.Name.ToUpper()); } if (allowedMethods.Count == 0) { continue; } allowedVerbs = string.Join(" ", allowedMethods.ToArray()); } routes.AddRoute(requestType, allowedVerbs); } } }
private static void AddOldApiRoutes(IServiceRoutes routes, Assembly assembly) { var services = assembly.GetExportedTypes() .Where(t => !t.IsAbstract && t.IsSubclassOfRawGeneric(typeof(ServiceBase <>))); foreach (Type service in services) { Type baseType = service.BaseType; //go up the hierarchy to the first generic base type while (!baseType.IsGenericType) { baseType = baseType.BaseType; } Type requestType = baseType.GetGenericArguments()[0]; string allowedVerbs = null; //null == All Routes if (service.IsSubclassOfRawGeneric(typeof(RestServiceBase <>))) { //find overriden REST methods var allowedMethods = new List <string>(); if (service.GetMethod("OnGet").DeclaringType == service) { allowedMethods.Add(HttpMethods.Get); } if (service.GetMethod("OnPost").DeclaringType == service) { allowedMethods.Add(HttpMethods.Post); } if (service.GetMethod("OnPut").DeclaringType == service) { allowedMethods.Add(HttpMethods.Put); } if (service.GetMethod("OnDelete").DeclaringType == service) { allowedMethods.Add(HttpMethods.Delete); } if (service.GetMethod("OnPatch").DeclaringType == service) { allowedMethods.Add(HttpMethods.Patch); } if (allowedMethods.Count == 0) { continue; } allowedVerbs = string.Join(" ", allowedMethods.ToArray()); } routes.AddRoute(requestType, allowedVerbs); } }