Example #1
0
        private static void AddActionConstraints(
            ReflectedActionDescriptor actionDescriptor,
            ReflectedActionModel action,
            ReflectedControllerModel controller)
        {
            var httpMethods = action.HttpMethods;

            if (httpMethods != null && httpMethods.Count > 0)
            {
                actionDescriptor.MethodConstraints = new List <HttpMethodConstraint>()
                {
                    new HttpMethodConstraint(httpMethods)
                };
            }

            actionDescriptor.RouteConstraints.Add(new RouteDataActionConstraint(
                                                      "controller",
                                                      controller.ControllerName));

            if (action.IsActionNameMatchRequired)
            {
                actionDescriptor.RouteConstraints.Add(new RouteDataActionConstraint(
                                                          "action",
                                                          action.ActionName));
            }
            else
            {
                actionDescriptor.RouteConstraints.Add(new RouteDataActionConstraint(
                                                          "action",
                                                          RouteKeyHandling.DenyKey));
            }
        }
Example #2
0
        public void ReflectedControllerModel_ComputesControllerName_WithoutSuffix()
        {
            // Arrange
            var controllerType = typeof(Store);

            // Act
            var model = new ReflectedControllerModel(controllerType.GetTypeInfo());

            // Assert
            Assert.Equal("Store", model.ControllerName);
        }
Example #3
0
        public void ReflectedControllerModel_ComputesControllerName()
        {
            // Arrange
            var controllerType = typeof(BlogController);

            // Act
            var model = new ReflectedControllerModel(controllerType.GetTypeInfo());

            // Assert
            Assert.Equal("Blog", model.ControllerName);
        }
        public void ReflectedControllerModel_ComputesControllerName_WithoutSuffix()
        {
            // Arrange
            var controllerType = typeof(Store);

            // Act
            var model = new ReflectedControllerModel(controllerType.GetTypeInfo());

            // Assert
            Assert.Equal("Store", model.ControllerName);
        }
        public void ReflectedControllerModel_ComputesControllerName()
        {
            // Arrange
            var controllerType = typeof(BlogController);

            // Act
            var model = new ReflectedControllerModel(controllerType.GetTypeInfo());

            // Assert
            Assert.Equal("Blog", model.ControllerName);
        }
Example #6
0
        public void ReflectedControllerModel_PopulatesRouteConstraintAttributes()
        {
            // Arrange
            var controllerType = typeof(BlogController);

            // Act
            var model = new ReflectedControllerModel(controllerType.GetTypeInfo());

            // Assert
            Assert.Single(model.RouteConstraints);
            Assert.IsType <MyRouteConstraintAttribute>(model.RouteConstraints[0]);
        }
Example #7
0
        public void ReflectedControllerModel_PopulatesFilters()
        {
            // Arrange
            var controllerType = typeof(BlogController);

            // Act
            var model = new ReflectedControllerModel(controllerType.GetTypeInfo());

            // Assert
            Assert.Single(model.Filters);
            Assert.IsType <MyFilterAttribute>(model.Filters[0]);
        }
        public void ReflectedControllerModel_PopulatesRouteConstraintAttributes()
        {
            // Arrange
            var controllerType = typeof(BlogController);

            // Act
            var model = new ReflectedControllerModel(controllerType.GetTypeInfo());

            // Assert
            Assert.Single(model.RouteConstraints);
            Assert.IsType<MyRouteConstraintAttribute>(model.RouteConstraints[0]);
        }
        public void ReflectedControllerModel_PopulatesFilters()
        {
            // Arrange
            var controllerType = typeof(BlogController);

            // Act
            var model = new ReflectedControllerModel(controllerType.GetTypeInfo());

            // Assert
            Assert.Single(model.Filters);
            Assert.IsType<MyFilterAttribute>(model.Filters[0]);
        }
Example #10
0
        public void ReflectedControllerModel_PopulatesAttributeRouteInfo()
        {
            // Arrange
            var controllerType = typeof(BlogController);

            // Act
            var model = new ReflectedControllerModel(controllerType.GetTypeInfo());

            // Assert
            Assert.NotNull(model.AttributeRoutes);
            Assert.Equal(2, model.AttributeRoutes.Count);;
            Assert.Single(model.AttributeRoutes, r => r.Template.Equals("Blog"));
            Assert.Single(model.AttributeRoutes, r => r.Template.Equals("Microblog"));
        }
Example #11
0
        public ReflectedApplicationModel BuildModel()
        {
            var applicationModel = new ReflectedApplicationModel();

            applicationModel.Filters.AddRange(_globalFilters);

            var assemblies      = _controllerAssemblyProvider.CandidateAssemblies;
            var types           = assemblies.SelectMany(a => a.DefinedTypes);
            var controllerTypes = types.Where(_conventions.IsController);

            foreach (var controllerType in controllerTypes)
            {
                var controllerModel = new ReflectedControllerModel(controllerType);
                applicationModel.Controllers.Add(controllerModel);

                foreach (var methodInfo in controllerType.AsType().GetMethods())
                {
                    var actionInfos = _conventions.GetActions(methodInfo, controllerType);
                    if (actionInfos == null)
                    {
                        continue;
                    }

                    foreach (var actionInfo in actionInfos)
                    {
                        var actionModel = new ReflectedActionModel(methodInfo);

                        actionModel.ActionName = actionInfo.ActionName;
                        actionModel.IsActionNameMatchRequired = actionInfo.RequireActionNameMatch;
                        actionModel.HttpMethods.AddRange(actionInfo.HttpMethods ?? Enumerable.Empty <string>());

                        if (actionInfo.AttributeRoute != null)
                        {
                            actionModel.AttributeRouteModel = new ReflectedAttributeRouteModel(
                                actionInfo.AttributeRoute);
                        }

                        foreach (var parameter in methodInfo.GetParameters())
                        {
                            actionModel.Parameters.Add(new ReflectedParameterModel(parameter));
                        }

                        controllerModel.Actions.Add(actionModel);
                    }
                }
            }

            return(applicationModel);
        }
Example #12
0
        public void ReflectedControllerModel_PopulatesAttributes()
        {
            // Arrange
            var controllerType = typeof(BlogController);

            // Act
            var model = new ReflectedControllerModel(controllerType.GetTypeInfo());

            // Assert
            Assert.Equal(3, model.Attributes.Count);

            Assert.Single(model.Attributes, a => a is MyOtherAttribute);
            Assert.Single(model.Attributes, a => a is MyFilterAttribute);
            Assert.Single(model.Attributes, a => a is MyRouteConstraintAttribute);
        }
        public void ReflectedControllerModel_PopulatesAttributes()
        {
            // Arrange
            var controllerType = typeof(BlogController);

            // Act
            var model = new ReflectedControllerModel(controllerType.GetTypeInfo());

            // Assert
            Assert.Equal(3, model.Attributes.Count);

            Assert.Single(model.Attributes, a => a is MyOtherAttribute);
            Assert.Single(model.Attributes, a => a is MyFilterAttribute);
            Assert.Single(model.Attributes, a => a is MyRouteConstraintAttribute);
        }
Example #14
0
        public void ReflectedControllerModel_PopulatesAttributes()
        {
            // Arrange
            var controllerType = typeof(BlogController);

            // Act
            var model = new ReflectedControllerModel(controllerType.GetTypeInfo());

            // Assert
            Assert.Equal(5, model.Attributes.Count);

            Assert.Single(model.Attributes, a => a is MyOtherAttribute);
            Assert.Single(model.Attributes, a => a is MyFilterAttribute);
            Assert.Single(model.Attributes, a => a is MyRouteConstraintAttribute);

            var routes = model.Attributes.OfType <RouteAttribute>().ToList();

            Assert.Equal(2, routes.Count());
            Assert.Single(routes, r => r.Template.Equals("Blog"));
            Assert.Single(routes, r => r.Template.Equals("Microblog"));
        }
Example #15
0
        private static IList <ReflectedActionDescriptor> CreateActionDescriptors(
            ReflectedActionModel action,
            ReflectedControllerModel controller,
            ControllerDescriptor controllerDescriptor)
        {
            var actionDescriptors = new List <ReflectedActionDescriptor>();

            // We check the action to see if the template allows combination behavior
            // (It doesn't start with / or ~/) so that in the case where we have multiple
            // [Route] attributes on the controller we don't end up creating multiple
            // attribute identical attribute routes.
            if (controller.AttributeRoutes != null &&
                controller.AttributeRoutes.Count > 0 &&
                (action.AttributeRouteModel == null ||
                 !action.AttributeRouteModel.IsAbsoluteTemplate))
            {
                foreach (var controllerAttributeRoute in controller.AttributeRoutes)
                {
                    var actionDescriptor = CreateActionDescriptor(
                        action,
                        controllerAttributeRoute,
                        controllerDescriptor);

                    actionDescriptors.Add(actionDescriptor);
                }
            }
            else
            {
                actionDescriptors.Add(CreateActionDescriptor(
                                          action,
                                          controllerAttributeRoute: null,
                                          controllerDescriptor: controllerDescriptor));
            }

            return(actionDescriptors);
        }