Skip to content

GetEndpointAttribute<T> in EndpointHttpContextExtensions.cs #37425

Open
@danleonard-nj

Description

@danleonard-nj

Is your feature request related to a problem?

Not a problem, just a nice-to-have. It'd be convenient to be able to fetch route attributes directly from HttpContext. I've found it comes up pretty often when working with custom middleware, it's not a long path to get to the metadata from the context itself as it is now, but as there's already extensions to fetch the endpoint itself from HttpContext, it may also be useful to include something like this and maybe another extension to fetch a collection of route attributes.

Describe the solution you'd like

Perhaps something like GetEndpointAttribute<T> and GetEndpointAttributes<T> in EndpointHttpContextExtensions.cs alongside the other endpoint-related extensions.

Additional context

This is the basic idea (minus handling nulls, etc - that'd be handled the same way it is right now in GetEndpoint<T>)

public static T GetEndpointAttribute<T>(this HttpContext httpContext) where T : Attribute
{
  var attribute = httpContext
    .Features
    .Get<IEndpointFeature>()
    .Endpoint
    .Metadata.GetMetadata<T>();
  
  return attribute;
}

Metadata

Metadata

Assignees

Labels

api-needs-workAPI needs work before it is approved, it is NOT ready for implementationarea-minimalIncludes minimal APIs, endpoint filters, parameter binding, request delegate generator etc

Type

No type

Projects

Relationships

None yet

Development

No branches or pull requests

Issue actions