using System.Collections.Generic; using System.Linq; using ASC.Web.Api.Routing; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Routing; using Microsoft.AspNetCore.Routing.Patterns; using Microsoft.Extensions.Primitives; namespace ASC.Api.Core.Core { public class CustomEndpointDataSource : EndpointDataSource { public EndpointDataSource Source { get; } public override IReadOnlyList Endpoints { get; } public CustomEndpointDataSource(EndpointDataSource source) { Source = source; var endpoints = Source.Endpoints.Cast(); Endpoints = endpoints .Where(r => { var attr = r.Metadata.OfType().FirstOrDefault(); return attr == null || !attr.DisableFormat; }) .Select(r => new RouteEndpoint(r.RequestDelegate, RoutePatternFactory.Parse(r.RoutePattern.RawText + ".{format}"), r.Order, r.Metadata, r.DisplayName)) .ToList(); } public override IChangeToken GetChangeToken() { return Source.GetChangeToken(); } } public static class EndpointExtension { public static IEndpointRouteBuilder MapCustom(this IEndpointRouteBuilder endpoints) { endpoints.DataSources.Add(new CustomEndpointDataSource(endpoints.DataSources.First())); return endpoints; } } }