Example #1
0
        public static IEndpointConventionBuilder MapMvcRoute <TController>(
            this IEndpointRouteBuilder routeBuilder,
            string name,
            string template,
            object defaults,
            object constraints,
            object dataTokens) where TController : ControllerBase
        {
            var mvcEndpointDataSource = routeBuilder.DataSources.OfType <MvcEndpointDataSource>().FirstOrDefault();

            if (mvcEndpointDataSource == null)
            {
                mvcEndpointDataSource = routeBuilder.ServiceProvider.GetRequiredService <MvcEndpointDataSource>();
                routeBuilder.DataSources.Add(mvcEndpointDataSource);
            }

            var endpointInfo = new MvcEndpointInfo(
                name,
                template,
                new RouteValueDictionary(defaults),
                new RouteValueDictionary(constraints),
                new RouteValueDictionary(dataTokens),
                routeBuilder.ServiceProvider.GetRequiredService <ParameterPolicyFactory>());

            endpointInfo.ControllerType = typeof(TController);

            mvcEndpointDataSource.ConventionalEndpointInfos.Add(endpointInfo);

            return(endpointInfo);
        }
Example #2
0
        /// <summary>
        /// Adds MVC to the <see cref="IApplicationBuilder"/> request execution pipeline.
        /// </summary>
        /// <param name="app">The <see cref="IApplicationBuilder"/>.</param>
        /// <param name="configureRoutes">A callback to configure MVC routes.</param>
        /// <returns>A reference to this instance after the operation has completed.</returns>
        public static IApplicationBuilder UseMvc(
            this IApplicationBuilder app,
            Action <IRouteBuilder> configureRoutes)
        {
            if (app == null)
            {
                throw new ArgumentNullException(nameof(app));
            }

            if (configureRoutes == null)
            {
                throw new ArgumentNullException(nameof(configureRoutes));
            }

            VerifyMvcIsRegistered(app);

            var options = app.ApplicationServices.GetRequiredService <IOptions <MvcOptions> >();

            if (options.Value.EnableEndpointRouting)
            {
                var mvcEndpointDataSource = app.ApplicationServices
                                            .GetRequiredService <IEnumerable <EndpointDataSource> >()
                                            .OfType <MvcEndpointDataSource>()
                                            .First();
                var constraintResolver = app.ApplicationServices
                                         .GetRequiredService <IInlineConstraintResolver>();

                var endpointRouteBuilder = new EndpointRouteBuilder(app);

                configureRoutes(endpointRouteBuilder);

                foreach (var router in endpointRouteBuilder.Routes)
                {
                    // Only accept Microsoft.AspNetCore.Routing.Route when converting to endpoint
                    // Sub-types could have additional customization that we can't knowingly convert
                    if (router is Route route && router.GetType() == typeof(Route))
                    {
                        var endpointInfo = new MvcEndpointInfo(
                            route.Name,
                            route.RouteTemplate,
                            route.Defaults,
                            route.Constraints.ToDictionary(kvp => kvp.Key, kvp => (object)kvp.Value),
                            route.DataTokens,
                            constraintResolver);

                        mvcEndpointDataSource.ConventionalEndpointInfos.Add(endpointInfo);
                    }
                    else
                    {
                        throw new InvalidOperationException($"Cannot use '{router.GetType().FullName}' with Endpoint Routing.");
                    }
                }