public static IApplicationBuilder UseUcAspRouter(this IApplicationBuilder app, Action <IRouterBuilder> action) { var router = new EndpointRouteBuilder(app); action(router); return(app.UseMiddleware <RouterMiddleware>(router)); }
/// <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."); } }
public EndpointMiddlewareFacde() { this.endpointRouteBuilder = new EndpointRouteBuilder(); this.endpointRoute = new EndpointRoute(); this.endpointHandler = new EndpointHandler(); }