public static RouteActionMatch SearchRouteActionMatch(string[] actionPathSegments, HttpVerb method, Type controllerType) { Assert.NotNull(actionPathSegments, nameof(actionPathSegments)); Assert.NotNull(controllerType, nameof(controllerType)); Assert.ForEach(actionPathSegments, s => !string.IsNullOrWhiteSpace(s), "actionPathSegment cannot be null empty or whitespace."); Type methodAttributeType = supportedMethodTypeMapping[method]; var methodMatches = controllerType.GetMethods(BindingFlags.Public | BindingFlags.Instance) .Where(m => m.IsDefined(methodAttributeType, false) && m.ReturnType == typeof(IActionResult)) .Select(m => new { Method = m, Attr = (HttpMethodAttribute)m.GetCustomAttribute(methodAttributeType) }) .ToArray(); var match = methodMatches .FirstOrDefault(match => RouteActionMatch.RequestPathMatchesRouteTemplate(match.Attr.GetTemplatePathSegments(), actionPathSegments)); if (match != null) { return(new RouteActionMatch(controllerType, match.Method, match.Attr)); } else { throw new NotFoundException($"Couldn´t find an appropriate Action for ActionPath:{string.Join('/', actionPathSegments)} and HttpVerb: {method}"); } }
public RouteMatch(RouteActionMatch actionMatch, string matchingPath) { TemplatePathSegments = actionMatch.HttpMethod.GetTemplatePathSegments(); ActionMatch = actionMatch; MatchingPath = matchingPath ?? throw new ArgumentNullException(nameof(matchingPath)); }
public RouteMatch GetEndPointHandler(RequestContext context) { if (context == null) { throw new ArgumentNullException(nameof(context)); } if (context.Request == null) { throw new ArgumentNullException($"{nameof(context)}.{nameof(context.Request)}"); } if (string.IsNullOrWhiteSpace(context.Request.Path)) { throw new ArgumentNullException($"{nameof(context)}.{nameof(context.Request)}.{nameof(context.Request.Path)}"); } string requestPath = context.Request.Path; string[] requestPathSegments = requestPath .Split("/") .Skip(1) .ToArray(); if (requestPathSegments.Length > 0) { string controller = requestPathSegments[0]; if (controller == string.Empty) { //Todo: default e.g.: HomeController } Type controllerType = registeredEndpointHandlerTypes .FirstOrDefault(endPoint => IsRequestedControllerType(controller, endPoint)); if (controllerType == null) { NotFoundException innerException = new NotFoundException( $"Die angeforderte Resource:{controller} konnte nicht gefunden werden."); throw new EndpointHandlerRegisterException( "Error resolving EndpointHandler. See InnerException.", innerException); } string[] actionPathSegments = requestPathSegments .Skip(1) .Where(segment => !string.IsNullOrWhiteSpace(segment)) .ToArray(); try { RouteActionMatch actionMatch = EndpointControllerReflector.SearchRouteActionMatch(actionPathSegments, context.Request.Method, controllerType); return(new RouteMatch(actionMatch, requestPath)); } catch (NotFoundException nfEx) { throw new EndpointHandlerRegisterException($"Die angeforderte Resource:{controllerType} konnte unter dem Pfad:{requestPath} nicht gefunden werden.", nfEx); } } else { FormatException innerException = new FormatException($"Invalid RequestFormat: {context.Request}"); throw new EndpointHandlerRegisterException( "Error resolving EndpointHandler. See InnerException.", innerException); } }