dotnet / dotnet/aspnetcore

Generic version of ConsumesAttribute

Open
#67,701 1 comment 1 reaction 0 assignees View on GitHub
api-proposal api-suggestion area-mvc
Dominant language
C#
Stars
38.4k
Forks
10.9k
Avg merge
2d 5h
Merged PRs (30d)
276

Description

## Background and Motivation

This was mentioned in this issue but it looks like it didn't make it in. https://github.com/dotnet/aspnetcore/issues/37767

## Proposed API

```diff
namespace Microsoft.AspNetCore.Mvc;

+[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = false, Inherited = true)]
+public class ConsumesAttribute : ConsumesAttribute
+{
+ public ConsumesAttribute(string contentType) : base(typeof(T), contentType)
+ { }
+}
```

## Usage Examples

```csharp
[Consumes("application/x-www-form-urlencoded")]
[EndpointName(nameof(AddTodo))]
[Tags("TodoApi")]
async Task AddTodo(Todo todo, TodoDb db)
{
if (!MiniValidator.TryValidate(todo, out var errors))
return Results.ValidationProblem(errors);

db.Todos.Add(todo);
await db.SaveChangesAsync();

return Results.Created($"/todos/{todo.Id}", todo);
}
```

## Alternative Designs

## Risks

Contributor guide

Open the contributing guide

Research direction

Start by reading the existing ConsumesAttribute API and the related discussion in issue 37767. Compare the proposed generic API and usage example with current conventions, then verify that the new public API builds and that its attribute usage behaves as shown.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp
Domain
api, backend-api-design
Issue type
Feature
Difficulty
3/5
Estimated time
1-2 days
Activity status
Active
Clarity
Mostly clear
Newbie friendliness
68/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.