Exemplo n.º 1
0
        public void TestTextMethodMatchFail()
        {
            var textMethod = testControllerType.GetMethod("Text");
            var route      = new BareApiRoute("test/{text}/az", textMethod, configMock.Object);

            Assert.False(route.Match(new[] { "test", "toto" }), "route match");
        }
Exemplo n.º 2
0
        private void LoadAssembly(Assembly assembly)
        {
            foreach (var c in assembly.DefinedTypes.Where(t => t.IsClass && !t.IsAbstract && typeof(IController).IsAssignableFrom(t)))
            {
                var prefix = c.GetCustomAttribute <RoutePrefixAttribute>();
                if (prefix == null)
                {
                    continue;
                }
                foreach (var method in c.GetMethods().Where(t => t.GetCustomAttribute <NonActionAttribute>() == null))
                {
                    var routeAttribute = method.GetCustomAttribute <RouteAttribute>();
                    if (routeAttribute == null)
                    {
                        continue;
                    }
                    var route = routeAttribute.Format ?? "";
                    if (route.IndexOf("~", StringComparison.Ordinal) == 0)
                    {
                        route = route.Substring(1);
                    }
                    else if (route.Length > 0)
                    {
                        route = prefix.Name + "/" + route;
                    }
                    else
                    {
                        route = prefix.Name;
                    }

                    string methodName;
                    if (method.GetCustomAttribute <HttpPostAttribute>() != null)
                    {
                        methodName = "POST";
                    }
                    else if (method.GetCustomAttribute <HttpPutAttribute>() != null)
                    {
                        methodName = "PUT";
                    }
                    else if (method.GetCustomAttribute <HttpDeleteAttribute>() != null)
                    {
                        methodName = "DELETE";
                    }
                    else
                    {
                        methodName = "GET";
                    }
                    if (!routes.ContainsKey(methodName))
                    {
                        routes.Add(methodName, new List <AbstractRoute>());
                    }

                    AbstractRoute newRoute;
                    if (method.ReturnType.IsGenericType && method.ReturnType.GetGenericTypeDefinition() == typeof(Task <>))
                    {
                        var innerType = method.ReturnType.GetGenericArguments()[0];
                        if (innerType == typeof(IHttpActionResult))
                        {
                            newRoute = new AsyncHttpActionResultApiRoute(route, method, this);
                        }
                        else if (typeof(IHttpActionResult).IsAssignableFrom(innerType))
                        {
                            // TODO not very nice. Need JIT?
                            var constructorInfo = typeof(AsyncHttpActionResultApiRoute <>).MakeGenericType(innerType)
                                                  .GetConstructor(new[] { route.GetType(), method.GetType(), this.GetType() });
                            if (constructorInfo != null)
                            {
                                newRoute = (AbstractRoute)
                                           constructorInfo
                                           .Invoke(new object[] { route, method, this });
                            }
                            else
                            {
                                throw new Exception("Constructor not found");
                            }
                        }
                        else
                        {
                            throw new NotImplementedException("Task return type");
                        }
                    }
                    else if (typeof(IHttpActionResult).IsAssignableFrom(method.ReturnType) || (method.ReturnType.IsGenericType && method.ReturnType.GetGenericTypeDefinition() == typeof(IHttpActionResult <>)))
                    {
                        newRoute = new HttpActionResultApiRoute(route, method, this);
                    }
                    else
                    {
                        newRoute = new BareApiRoute(route, method, this);
                    }
                    if (routeAttribute.Name != null)
                    {
                        routesByName[routeAttribute.Name] = newRoute;
                    }
                    routes[methodName].Add(newRoute);
                }
            }

            foreach (var c in assembly.DefinedTypes.Where(t => t.IsClass && !t.IsAbstract && typeof(IView).IsAssignableFrom(t)))
            {
                var name   = c.Name;
                var suffix = name.IndexOf("View", StringComparison.Ordinal);
                if (suffix > 0)
                {
                    name = name.Substring(0, suffix);
                }
                var constructor = c.GetConstructor(Type.EmptyTypes);
                if (constructor != null)
                {
                    views.Add(name, (IView)constructor.Invoke(null));
                }
            }
        }