Exemplo n.º 1
0
 public void Add(FluentActionCollection fluentActions)
 {
     foreach (var fluentAction in fluentActions)
     {
         PreConfigureAction(fluentAction);
         FluentActions.Add(fluentAction);
     }
 }
        public static IApplicationBuilder UseFluentActions(
            this IApplicationBuilder app,
            FluentActionCollection fluentActions)
        {
            if (fluentActions == null)
            {
                throw new ArgumentNullException(nameof(fluentActions));
            }

            var controllerDefinitionBuilder = new FluentActionControllerDefinitionBuilder();

            var controllerDefinitions = fluentActions
                                        .Select(fluentAction => controllerDefinitionBuilder.Build(fluentAction))
                                        .ToList();

            if (!controllerDefinitions.Any())
            {
                return(app);
            }

            var context = (FluentActionControllerFeatureProviderContext)app
                          .ApplicationServices
                          .GetService(typeof(FluentActionControllerFeatureProviderContext));

            if (context == null)
            {
                throw new Exception("Could not find a feature provider for fluent actions, did you remember to call app.AddMvc().AddFluentActions()?");
            }

            if (context.ControllerDefinitions == null)
            {
                context.ControllerDefinitions = controllerDefinitions;
            }
            else
            {
                context.ControllerDefinitions = context.ControllerDefinitions.Concat(controllerDefinitions);
            }

            app.UseEndpoints(routes =>
            {
                foreach (var controllerDefinition in controllerDefinitions
                         .Where(controllerDefinition => controllerDefinition.FluentAction.Definition.IsMapRoute))
                {
                    routes.MapControllerRoute(
                        controllerDefinition.Id,
                        controllerDefinition.RouteTemplate.WithoutLeading("/"),
                        new
                    {
                        controller = controllerDefinition.Name.WithoutTrailing("Controller"),
                        action     = controllerDefinition.ActionName
                    });
                }
            });

            return(app);
        }
Exemplo n.º 3
0
        public static FluentActionCollection DefineActions(Action <FluentActionCollection> addFluentActions)
        {
            var actionCollection = new FluentActionCollection(new FluentActionCollectionConfig());

            addFluentActions(actionCollection);

            actionCollection.PostConfigureActions();

            return(actionCollection);
        }
        public static IApplicationBuilder UseFluentActions(
            this IApplicationBuilder app,
            Action <FluentActionCollection> addFluentActions)
        {
            if (addFluentActions == null)
            {
                throw new ArgumentNullException(nameof(addFluentActions));
            }

            var fluentActions = FluentActionCollection.DefineActions(addFluentActions);

            return(app.UseFluentActions(fluentActions));
        }