asp.net-coreroutesendpoint

How to tell whether an Endpoint is an ApiController?


Does an Endpoint obtained from an EndpointDataSource contain any information that identifies the route as an ApiController?

XY: I want an IMiddleware to require a cookie for all requests except API controllers. Currently I'm testing whether the request path starts with "/api", but I wonder if there's a more robust/decoupled way to identify paths that match API controller routes.


Solution

  • The MetadataCollection list returned from endpoint.Metadata contains different entries based on whether an endpoint is an API route or a Razor Page route.

    By checking if the MetadataCollection of an Endpoint contains a specific class, you can determine the type of endpoint.

    Endpoint Class to search in MetadataCollection
    Razor Page A Razor Page route contains an entry of type Microsoft.AspNetCore.Mvc.ApplicationModels.PageRouteMetadata in its MetadataCollection. An API route will never contain this entry.
    API An API route will contain an entry of type Microsoft.AspNetCore.Mvc.Routing.HttpMethodAttribute in its MetadataCollection list.

    Detecting an API endpoint

    if (endpoint.Metadata.OfType<Microsoft.AspNetCore.Mvc.Routing.HttpMethodAttribute>().Any())
    {
        // This is an API route
    }
    

    The HttpMethodAttribute class is defined as:

    Identifies an action that supports a given set of HTTP methods.

    A MetadataCollection list could contain an entry of type endpoint.Metadata.OfType<Microsoft.AspNetCore.Mvc.RouteAttribute. This entry type, however, will be present only if the endpoint is decorated with [Route("api/mymethod")]. Not all API endpoints have a Route attribute set, as the Route attribute can be set at the Controller level.

    Detecting a Razor Page route

    if (endpoint.Metadata.OfType<Microsoft.AspNetCore.Mvc.ApplicationModels.PageRouteMetadata>().Any())
    {
        // This is a Razor Page route
    }
    

    The PageRouteMetadata class is defined as:

    Metadata used to construct an endpoint route to the page.