Skip to content

Add IgnoreApi() extension method for IEndpointConventionBuilder #34068

Closed
@martincostello

Description

@martincostello

Background and Motivation

Related to #34061, if using minimal hosting with minimal actions and Swashbuckle and it is possible to hide actions from API Explorer using the [ApiExplorerSettings] attribute (which doesn't work right now, see also #34065), it would be a quality-of-life improvement to the API surface to add an extension method for IEndpointConventionBuilder to make it require less verbose code to hide it.

Proposed API

Add a new extension method to the Microsoft.AspNetCore.Mvc.ApiExplorer assembly with a name something along the lines of IgnoreApi():

namespace Microsoft.AspNetCore.Builder
{
+    public static class ApiExplorerEndpointConventionBuilderExtensions
+    {
+       public static TBuilder IgnoreApi<TBuilder>(this TBuilder builder) where TBuilder : IEndpointConventionBuilder;
+    }
}

The implementation would be something like this:

using Microsoft.AspNetCore.Mvc;

namespace Microsoft.AspNetCore.Builder
{
    /// <summary>
    /// API Explorer extension methods for <see cref="IEndpointConventionBuilder"/>.
    /// </summary>
    public static class ApiExplorerEndpointConventionBuilderExtensions
    {
        private static readonly ApiExplorerSettingsAttribute _ignoreApiMetadata = new()
        {
            IgnoreApi = true
        };

        /// <summary>
        /// Ignores the endpoint(s) from the API Explorer.
        /// </summary>
        /// <param name="builder">The endpoint convention builder.</param>
        /// <returns>The original convention builder parameter.</returns>
        public static TBuilder IgnoreApi<TBuilder>(this TBuilder builder) where TBuilder : IEndpointConventionBuilder
        {
            builder.Add(endpointBuilder =>
            {
                endpointBuilder.Metadata.Add(_ignoreApiMetadata);
            });
            return builder;
        }
    }
}

Usage Examples

var builder = WebApplication.CreateBuilder(args);

// Configure application...

var app = builder.Build();

// Redirect to Swagger documentation
app.MapGet("/api", () => Results.Redirect("/swagger-ui/index.html"))
   .IgnoreApi();

// Configure other endpoints...

app.Run();

Alternative Designs

No alternative designs considered, this is just API sugar to remove the need to use verbose way of doing this using the existing APIs, similar to the existing AllowAnonymous(), RequireAuthorization(), RequireCors(), RequireHost() and WithDisplayName() extension methods:

builder.MapGet("/api", () => Results.Redirect("/swagger-ui/index.html"))
       .WithMetadata(new ApiExplorerSettingsAttribute { IgnoreApi = true });

Risks

None that I'm aware of, other than potential conflicts with user-defined application extension methods of the same name.

Metadata

Metadata

Assignees

Labels

Priority:1Work that is critical for the release, but we could probably ship withoutapi-suggestionEarly API idea and discussion, it is NOT ready for implementationarea-minimalIncludes minimal APIs, endpoint filters, parameter binding, request delegate generator etcfeature-minimal-actionsController-like actions for endpoint routingold-area-web-frameworks-do-not-use*DEPRECATED* This label is deprecated in favor of the area-mvc and area-minimal labels

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions