dotnet / dotnet/aspnetcore

GetEndpointAttribute<T> in EndpointHttpContextExtensions.cs

Open
#37,425 8 comments 3 reactions 0 assignees View on GitHub
api-needs-work area-minimal
Dominant language
C#
Stars
38.4k
Forks
10.9k
Avg merge
2d 6h
Merged PRs (30d)
290

Description

### 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` and `GetEndpointAttributes` 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`)

```csharp
public static T GetEndpointAttribute(this HttpContext httpContext) where T : Attribute
{
var attribute = httpContext
.Features
.Get()
.Endpoint
.Metadata.GetMetadata();

return attribute;
}
```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.