Skip to content

Add RoutePattern property to ApiDescription #64412

@captainsafia

Description

@captainsafia

Background and Motivation

ApiDescription today exposes only RelativePath as a string. Documentation tooling often has to re-parse that text to reconstruct routing details that endpoint routing already knows. This leads to mismatches, especially when templates use special prefixes (like ~) or when constraints/defaults can’t be inferred from the string. The goal here is to make the routing engine’s authoritative RoutePattern available to tools that build OpenAPI documents, generate API overviews, or analyze routes. Existing behavior of RelativePath remains unchanged.

#64408 surfaces the canonical RoutePattern from RouteEndpoint (and from attribute routes). This gives documentation generators a reliable, structured view of the actual routed pattern and eliminates re-parsing heuristics.

Proposed API

namespace Microsoft.AspNetCore.Mvc.ApiExplorer;

public class ApiDescription
{
+    public Microsoft.AspNetCore.Routing.Patterns.RoutePattern? RoutePattern { get; set; }
}

Usage Examples

var pattern = apiDescription.RoutePattern ?? RoutePatternFactory.Parse(apiDescription.RelativePath!);

Alternatives

  • We could instead store the RoutePattern in EndpointMetadata for implementations to dig up but it feels appropriate to have it at the top-level, especially if RelativePath is already there.

Risks

  • Nullability: Property is marked as nullable to support binary compute. Some providers won’t set RoutePattern; fallbacks need to be set in consuming code.
  • Layering: Microsoft.AspNetCore.Mvc.Abstractions now has a dependency on Microsoft.AspNetCore.Routing. It already has a dependency on Microsoft.AspNetCore.Routing.Abstractions.

Metadata

Metadata

Assignees

No one assigned

    Labels

    api-ready-for-reviewAPI is ready for formal API review - https://github.com/dotnet/apireviewsarea-mvcIncludes: MVC, Actions and Controllers, Localization, CORS, most templates

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions