/// <summary>Registers the rest paths described by requestType.</summary> /// /// <exception cref="NotSupportedException">Thrown when the requested operation is not supported.</exception> /// /// <param name="requestType">Type of the request.</param> public void RegisterRestPaths(Type requestType) { var attrs = TypeDescriptor.GetAttributes(requestType).OfType <RouteAttribute>(); foreach (RouteAttribute attr in attrs) { var restPath = new RestPath(requestType, attr.Path, attr.Verbs, attr.Summary, attr.Notes); var defaultAttr = attr as FallbackRouteAttribute; if (defaultAttr != null) { if (EndpointHost.Config != null) { if (EndpointHost.Config.FallbackRestPath != null) { throw new NotSupportedException(string.Format( "Config.FallbackRestPath is already defined. Only 1 [FallbackRoute] is allowed.")); } EndpointHost.Config.FallbackRestPath = (httpMethod, pathInfo, filePath) => { var pathInfoParts = RestPath.GetPathPartsForMatching(pathInfo); return(restPath.IsMatch(httpMethod, pathInfoParts) ? restPath : null); }; } continue; } if (!restPath.IsValid) { throw new NotSupportedException(string.Format( "RestPath '{0}' on Type '{1}' is not Valid", attr.Path, requestType.Name)); } RegisterRestPath(restPath); } }
/// <summary>Returns the first matching RestPath.</summary> /// /// <param name="httpMethod">.</param> /// <param name="pathInfo"> .</param> /// /// <returns>The rest path for request.</returns> public IRestPath GetRestPathForRequest(string httpMethod, string pathInfo) { var matchUsingPathParts = RestPath.GetPathPartsForMatching(pathInfo); List <RestPath> firstMatches; var yieldedHashMatches = RestPath.GetFirstMatchHashKeys(matchUsingPathParts); foreach (var potentialHashMatch in yieldedHashMatches) { if (!this.RestPathMap.TryGetValue(potentialHashMatch, out firstMatches)) { continue; } var bestScore = -1; foreach (var restPath in firstMatches) { var score = restPath.MatchScore(httpMethod, matchUsingPathParts); if (score > bestScore) { bestScore = score; } } if (bestScore > 0) { foreach (var restPath in firstMatches) { if (bestScore == restPath.MatchScore(httpMethod, matchUsingPathParts)) { return(restPath); } } } } var yieldedWildcardMatches = RestPath.GetFirstMatchWildCardHashKeys(matchUsingPathParts); foreach (var potentialHashMatch in yieldedWildcardMatches) { if (!this.RestPathMap.TryGetValue(potentialHashMatch, out firstMatches)) { continue; } var bestScore = -1; foreach (var restPath in firstMatches) { var score = restPath.MatchScore(httpMethod, matchUsingPathParts); if (score > bestScore) { bestScore = score; } } if (bestScore > 0) { foreach (var restPath in firstMatches) { if (bestScore == restPath.MatchScore(httpMethod, matchUsingPathParts)) { return(restPath); } } } } return(null); }