internal IEnumerable <ControllerActionDescriptor> GetDescriptors()
    {
        var controllerTypes = GetControllerTypes();
        var application     = _applicationModelFactory.CreateApplicationModel(controllerTypes);

        return(ControllerActionDescriptorBuilder.Build(application));
    }
예제 #2
0
        internal protected IEnumerable <ControllerActionDescriptor> GetDescriptors()
        {
            var applicationModel = BuildModel();

            ApplicationModelConventions.ApplyConventions(applicationModel, _conventions);
            return(ControllerActionDescriptorBuilder.Build(applicationModel));
        }
예제 #3
0
    public void Build_WithPropertiesSet_FromApplicationModel()
    {
        // Arrange
        var applicationModel = new ApplicationModel();

        applicationModel.Properties["test"] = "application";

        var controller = new ControllerModel(typeof(TestController).GetTypeInfo(),
                                             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
        Assert.Equal("application", descriptors.Single().Properties["test"]);
    }
예제 #4
0
    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);
    }
        internal protected IEnumerable <ControllerActionDescriptor> GetDescriptors()
        {
            var applicationModel = BuildModel();

            ApplicationModelConventions.ApplyConventions(applicationModel, _conventions);
            if (_logger.IsEnabled(LogLevel.Verbose))
            {
                foreach (var controller in applicationModel.Controllers)
                {
                    _logger.LogVerbose(new ControllerModelValues(controller));
                }
            }
            return(ControllerActionDescriptorBuilder.Build(applicationModel));
        }
예제 #6
0
        public static HttpHandler to_api_controller <TController>(string prefix = "", RouteValueDictionary defaultRouteValues = null)
        {
            return(HttpHandler.FromFunc(async ctx =>
            {
                var currentPath = prefix + GetPath(ctx);

                // REVIEW: cache all this?
                var appModelProvider = ctx.RequestServices.GetServices <IApplicationModelProvider>().OfType <DefaultApplicationModelProvider>().Single();
                ApplicationModelProviderContext appModelProviderCtx = new ApplicationModelProviderContext(new[] { typeof(TController).GetTypeInfo() });
                appModelProvider.OnProvidersExecuting(appModelProviderCtx);

                var descriptors = ControllerActionDescriptorBuilder.Build(appModelProviderCtx.Result);
                var matchedDescriptors = new List <ActionDescriptor>();
                var parsedValues = new Dictionary <ActionDescriptor, RouteValueDictionary>();

                foreach (var d in descriptors)
                {
                    var templateMatcher = new TemplateMatcher(TemplateParser.Parse(d.AttributeRouteInfo.Template), defaultRouteValues ?? new RouteValueDictionary());
                    var parsedRouteValues = new RouteValueDictionary();
                    if (templateMatcher.TryMatch(new PathString(currentPath), parsedRouteValues))
                    {
                        matchedDescriptors.Add(d);
                        parsedValues[d] = parsedRouteValues;
                    }
                }

                var actionSelector = ctx.RequestServices.GetRequiredService <IActionSelector>();
                var best = actionSelector.SelectBestCandidate(new RouteContext(ctx), matchedDescriptors);

                var routeCtx = new RouteContext(ctx);
                routeCtx.RouteData.Values.Clear();
                foreach (var kv in parsedValues[best])
                {
                    routeCtx.RouteData.Values.Add(kv.Key, kv.Value);
                }

                var actx = new ActionContext(ctx, routeCtx.RouteData, best);
                var actionInvokerProvider = ctx.RequestServices.GetServices <IActionInvokerProvider>().OfType <ControllerActionInvokerProvider>().Single();
                var fac = new ActionInvokerFactory(new[] { actionInvokerProvider });
                IActionInvoker invoker = fac.CreateInvoker(actx);
                await invoker.InvokeAsync();
                return true;
            }));
        }