public async Task <HttpResponse> Handle(HttpRequest request) { var url = request.Path; var httpMethod = request.Method; foreach (var route in RoutingMethods) { var requestPath = url.AbsolutePath; var routingMethod = ((HttpRequestMethod)route.GetCustomAttribute(typeof(HttpRequestMethod)))?.Method ?? HttpMethod.Get; var routingPath = RESTPath.Combine(Prefix, ((Route)route.GetCustomAttribute(typeof(Route))).Path); bool sameMethod = String.Equals(routingMethod.Method, httpMethod.Method); bool matchingUrl = routingPath.Matches(requestPath); if (sameMethod && matchingUrl) { var method = route; var parameters = ExtractParameters(method, routingPath, request); if (method.GetCustomAttribute(typeof(AsyncStateMachineAttribute)) != null) { return(await(Task <HttpResponse>) method.Invoke(this, parameters.ToArray())); } else { return((HttpResponse)method.Invoke(this, parameters.ToArray())); } } } return(NotFound($"Couldn't find a fitting method on the on matched controller '{ GetType().Name }' for path '{ url }'")); }
public async Task <HttpResponseBase> Handle(HttpRequest request) { var url = request.Path; var httpMethod = request.Method; var requestPath = Uri.UnescapeDataString(url.AbsolutePath); foreach (var method in PreMethods) { var parameters = new object[] { request }; // Debug.WriteLine($"{requestPath}: Calling pre-execution method ({method.Name})"); if (method.GetCustomAttribute(typeof(AsyncStateMachineAttribute)) != null) { await(Task) method.Invoke(this, parameters); } else { method.Invoke(this, parameters); } } try { foreach (var route in RoutingMethods) { var routingMethod = ((HttpRequestMethod)route.GetCustomAttribute(typeof(HttpRequestMethod)))?.Method ?? HttpMethod.Get; var routingPath = RESTPath.Combine(Prefix, ((Route)route.GetCustomAttribute(typeof(Route))).Path); bool sameMethod = String.Equals(routingMethod.Method, httpMethod.Method); bool matchingUrl = routingPath.PathReMatch.IsMatch(requestPath); if (sameMethod && matchingUrl) { var method = route; var parameters = ExtractParameters(method, routingPath, requestPath, request); Debug.WriteLine($"{requestPath}: Calling handler method ({method.Name})"); if (method.GetCustomAttribute(typeof(AsyncStateMachineAttribute)) != null) { return(await(Task <HttpResponseBase>) method.Invoke(this, parameters.ToArray())); } else { return((HttpResponseBase)method.Invoke(this, parameters.ToArray())); } } } foreach (var route in DefaultMethods) { var routingMethod = ((HttpRequestMethod)route.GetCustomAttribute(typeof(HttpRequestMethod)))?.Method ?? HttpMethod.Get; bool sameMethod = String.Equals(routingMethod.Method, httpMethod.Method); if (sameMethod) { var method = route; // Debug.WriteLine($"{requestPath}: Calling default handler method ({method.Name})"); if (method.GetCustomAttribute(typeof(AsyncStateMachineAttribute)) != null) { return(await(Task <HttpResponseBase>) method.Invoke(this, new[] { requestPath })); } else { return((HttpResponseBase)method.Invoke(this, new[] { requestPath })); } } } } finally { foreach (var method in PostMethods) { var parameters = new object[] { request }; // Debug.WriteLine($"{requestPath}: Calling post-execution method ({method.Name})"); if (method.GetCustomAttribute(typeof(AsyncStateMachineAttribute)) != null) { await(Task) method.Invoke(this, parameters); } else { method.Invoke(this, parameters); } } } return(NotFound($"Couldn't find a suitable method on controller '{GetType().Name}' for path '{url}'")); }