public void Apply(ApplicationModel application)
        {
            foreach (var controller in application.Controllers)
            {
                var type = controller.ControllerType.AsType();

                if (typeof(IApplicationService).IsAssignableFrom(type))
                {
                    controller.ControllerName = controller.ControllerName.RemovePostFix(ApplicationService.CommonPostfixes);

                    var configuration = GetControllerSettingOrNull(controller.ControllerType.AsType());
                    ConfigureArea(controller, configuration);
                    ConfigureRemoteService(controller, configuration);
                }
                else
                {
                    var remoteServiceAtt = ReflectionHelper.GetSingleAttributeOrDefault<RemoteServiceAttribute>(type);
                    if (remoteServiceAtt != null && remoteServiceAtt.IsEnabledFor(type))
                    {
                        var configuration = GetControllerSettingOrNull(controller.ControllerType.AsType());
                        ConfigureRemoteService(controller, configuration);
                    }
                }
            }
        }
        public void Build_WithPropertiesSet_ActionOverwritesApplicationAndControllerModel()
        {
            // Arrange
            var applicationModel = new ApplicationModel();
            applicationModel.Properties["test"] = "application";

            var controller = new ControllerModel(typeof(TestController).GetTypeInfo(),
                                                 new List<object>() { });
            controller.Application = applicationModel;
            controller.Properties["test"] = "controller";
            applicationModel.Controllers.Add(controller);

            var methodInfo = typeof(TestController).GetMethod(nameof(TestController.SomeAction));
            var actionModel = new ActionModel(methodInfo, new List<object>() { });
            actionModel.Selectors.Add(new SelectorModel());
            actionModel.Controller = controller;
            actionModel.Properties["test"] = "action";
            controller.Actions.Add(actionModel);

            // Act
            var descriptors = ControllerActionDescriptorBuilder.Build(applicationModel);

            // Assert
            Assert.Equal("action", descriptors.Single().Properties["test"]);
        }
        public void Apply(ApplicationModel application)
        {
            foreach (var controller in application.Controllers) {
                var hasAttributeRouteModels = controller.Selectors.Any(selector => selector.AttributeRouteModel != null);
                if (hasAttributeRouteModels) continue;

                var controllerTmpl = controller.ControllerName.PascalToSlug();
                controller.Selectors[0].AttributeRouteModel = new AttributeRouteModel { Template = controllerTmpl };

                var actionsToAdd = new List<ActionModel>();
                foreach (var action in controller.Actions) {
                    var hasAttributeRouteModel = action.Selectors.Any(selector => selector.AttributeRouteModel != null);
                    if (hasAttributeRouteModel) continue;
                    var actionSlug = action.ActionName.PascalToSlug();
                    var actionTmpl = $"{actionSlug}/{{id?}}";
                    if (actionSlug == DefaultAction) {
                        action.Selectors.Add(new SelectorModel { AttributeRouteModel = new AttributeRouteModel { Template = "" } });
                        if (controllerTmpl == DefaultController) {
                            action.Selectors.Add(new SelectorModel { AttributeRouteModel = new AttributeRouteModel { Template = "/" } });
                        }
                    }
                    action.Selectors[0].AttributeRouteModel = new AttributeRouteModel { Template = actionTmpl };
                }
            }
        }
 public void Apply(ApplicationModel application)
 {
     foreach (var controller in application.Controllers)
     {
         if (controller.ControllerType == _type)
         {
             controller.ApiExplorer.IsVisible = false;
         }
     }
 }
        public void Apply(ApplicationModel application)
        {
            foreach(var controller in application.Controllers)
            {
                var module = controller.ControllerType.Assembly.FullName;
                var splitIndex = module.IndexOf(',');
                if (splitIndex > 0)
                {
                    module = module.Substring(0, splitIndex);
                }

                controller.RouteConstraints.Add(new AreaAttribute(module));
            }
        }
 public void Apply(ApplicationModel application)
 {
     foreach (var controller in application.Controllers.Where(_controllerPredicate))
     {
         foreach (var action in controller.Actions.Where(_actionPredicate))
         {
             foreach (var parameter in action.Parameters.Where(parameter => parameter.BindingInfo?.BindingSource == null &&
                     !parameter.Attributes.OfType<IBindingSourceMetadata>().Any() &&
                     !parameter.ParameterInfo.ParameterType.CanBeConvertedFromString()).Where(_parameterPredicate))
             {
                 parameter.BindingInfo = parameter.BindingInfo ?? new BindingInfo();
                 parameter.BindingInfo.BindingSource = BindingSource.Body;
             }
         }
     }
 }
        public void Build_WithControllerPropertiesSet_AddsPropertiesWithBinderMetadataSet()
        {
            // Arrange
            var applicationModel = new ApplicationModel();
            var controller = new ControllerModel(
                typeof(TestController).GetTypeInfo(),
                new List<object>() { });

            var propertyInfo = controller.ControllerType.AsType().GetProperty("BoundProperty");
            controller.ControllerProperties.Add(
                new PropertyModel(
                    propertyInfo,
                    new List<object>() { })
                {
                    BindingInfo = BindingInfo.GetBindingInfo(new object[] { new FromQueryAttribute() }),
                    PropertyName = "BoundProperty"
                });

            controller.ControllerProperties.Add(
               new PropertyModel(
                   controller.ControllerType.AsType().GetProperty("UnboundProperty"),
                   new List<object>() { }));

            controller.Application = applicationModel;
            applicationModel.Controllers.Add(controller);

            var methodInfo = typeof(TestController).GetMethod(nameof(TestController.SomeAction));
            var actionModel = new ActionModel(methodInfo, new List<object>() { });
            actionModel.Selectors.Add(new SelectorModel());
            actionModel.Controller = controller;
            controller.Actions.Add(actionModel);

            // Act
            var descriptors = ControllerActionDescriptorBuilder.Build(applicationModel);

            // Assert
            var controllerDescriptor = Assert.Single(descriptors);

            var parameter = Assert.Single(controllerDescriptor.BoundProperties);
            var property = Assert.IsType<ControllerBoundPropertyDescriptor>(parameter);
            Assert.Equal("BoundProperty", property.Name);
            Assert.Equal(propertyInfo, property.PropertyInfo);
            Assert.Equal(typeof(string), property.ParameterType);
            Assert.Equal(BindingSource.Query, property.BindingInfo.BindingSource);
        }
        public void DefaultControllerModelConvention_AppliesToAllControllers()
        {
            // Arrange
            var options = new MvcOptions();
            var app = new ApplicationModel();
            app.Controllers.Add(new ControllerModel(typeof(HelloController).GetTypeInfo(), new List<object>()));
            app.Controllers.Add(new ControllerModel(typeof(WorldController).GetTypeInfo(), new List<object>()));
            options.Conventions.Add(new SimpleControllerConvention());

            // Act
            options.Conventions[0].Apply(app);

            // Assert
            foreach (var controller in app.Controllers)
            {
                Assert.True(controller.Properties.ContainsKey("TestProperty"));
            }
        }
        public void Apply(ApplicationModel application)
        {
            foreach (var controller in application.Controllers)
            {
                var type = controller.ControllerType.AsType();

                if (typeof(IApplicationService).IsAssignableFrom(type))
                {
                    controller.ControllerName = controller.ControllerName.RemovePostFix("AppService", "ApplicationService", "Service");
                    ConfigureRemoteService(controller);
                }
                else
                {
                    var remoteServiceAtt = ReflectionHelper.GetSingleAttributeOrDefault<RemoteServiceAttribute>(type);
                    if (remoteServiceAtt != null && remoteServiceAtt.IsEnabledFor(type))
                    {
                        ConfigureRemoteService(controller);
                    }
                }
            }
        }
        public void Apply(ApplicationModel application)
        {
            var centralPrefix = new AttributeRouteModel(new RouteAttribute($"{routingModel.Prefix}/{routingModel.Controller}"));
            foreach (var controller in application.Controllers)
            {
                if (controller.ControllerType.UnderlyingSystemType == typeof(AppConfigurationController))
                {
                    var hasRouteAttributes = controller.Selectors.Any(selector => selector.AttributeRouteModel != null);
                    if (hasRouteAttributes)
                    {
                        // This controller manually defined some routes, so treat this 
                        // as an override and not apply the convention here.
                        continue;
                    }


                    foreach (var selector in controller.Selectors)
                    {
                        selector.AttributeRouteModel = centralPrefix;
                    }
                }
            }
        }
        public void Apply(ApplicationModel application)
        {
            foreach (var controller in application.Controllers)
            {
                var matchedSelectors = controller.Selectors.Where(x => x.AttributeRouteModel != null).ToList();
                if (matchedSelectors.Any())
                {
                    foreach (var selectorModel in matchedSelectors)
                    {
                        selectorModel.AttributeRouteModel = AttributeRouteModel.CombineAttributeRouteModel(_centralPrefix,
                            selectorModel.AttributeRouteModel);
                    }
                }

                var unmatchedSelectors = controller.Selectors.Where(x => x.AttributeRouteModel == null).ToList();
                if (unmatchedSelectors.Any())
                {
                    foreach (var selectorModel in unmatchedSelectors)
                    {
                        selectorModel.AttributeRouteModel = _centralPrefix;
                    }
                }
            }
        }
            /// <inheritdoc />
            public void Apply(ApplicationModel application)
            {
                if (application == null)
                {
                    throw new ArgumentNullException(nameof(application));
                }

                foreach (var controller in application.Controllers)
                {
                    foreach (var action in controller.Actions)
                    {
                        _actionModelConvention.Apply(action);
                    }
                }
            }
Ejemplo n.º 13
0
 public void Apply(ApplicationModel application)
 {
     application.Properties["description"] = _description;
 }
Ejemplo n.º 14
0
        public static List <TResult> Flatten <TResult>(
            ApplicationModel application,
            Func <ApplicationModel, ControllerModel, ActionModel, SelectorModel, TResult> flattener)
        {
            var results = new List <TResult>();
            var errors  = new Dictionary <MethodInfo, IList <string> >();

            var actionsByMethod    = new Dictionary <MethodInfo, List <(ActionModel, SelectorModel)> >();
            var actionsByRouteName = new Dictionary <string, List <(ActionModel, SelectorModel)> >(StringComparer.OrdinalIgnoreCase);

            var routeTemplateErrors = new List <string>();

            foreach (var controller in application.Controllers)
            {
                foreach (var action in controller.Actions)
                {
                    foreach (var selector in ActionAttributeRouteModel.FlattenSelectors(action))
                    {
                        // PostProcess attribute routes so we can observe any errors.
                        ReplaceAttributeRouteTokens(controller, action, selector, routeTemplateErrors);

                        // Add to the data structures we use to find errors.
                        AddActionToMethodInfoMap(actionsByMethod, action, selector);
                        AddActionToRouteNameMap(actionsByRouteName, action, selector);

                        var result = flattener(application, controller, action, selector);
                        Debug.Assert(result != null);

                        results.Add(result);
                    }
                }
            }

            var attributeRoutingConfigurationErrors = new Dictionary <MethodInfo, string>();

            foreach (var(method, actions) in actionsByMethod)
            {
                ValidateActionGroupConfiguration(
                    method,
                    actions,
                    attributeRoutingConfigurationErrors);
            }

            if (attributeRoutingConfigurationErrors.Count > 0)
            {
                var message = CreateAttributeRoutingAggregateErrorMessage(attributeRoutingConfigurationErrors.Values);

                throw new InvalidOperationException(message);
            }

            var namedRoutedErrors = ValidateNamedAttributeRoutedActions(actionsByRouteName);

            if (namedRoutedErrors.Count > 0)
            {
                var message = CreateAttributeRoutingAggregateErrorMessage(namedRoutedErrors);
                throw new InvalidOperationException(message);
            }

            if (routeTemplateErrors.Count > 0)
            {
                var message = CreateAttributeRoutingAggregateErrorMessage(routeTemplateErrors);
                throw new InvalidOperationException(message);
            }


            return(results);
        }