dotnet / dotnet/aspnetcore

Support object content along with custom status code in Results API

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

Description

## Background and Motivation

Allow return custom response object along with a specific HTTP status code. Currently ```Results``` class supports only returning custom status code using ```Results.StatusCode(int)``` would be great if this method can be extended to include custom object as part of response.

Today it is possible to achieve this using other APIs such as ```Results.Json(Object, JsonSerializerOptions, String, Nullable)``` but this always uses JSON serialization. And it's not useful if minimal supports another serialization e.g., XML.

## Proposed API

```diff
Results.Status(int statusCode, object object) //produces a response with given status code and content
```

## Usage Examples

```csharp
internal record Error(int ErrorCode, string Message, string HelpLink);

Results.Status(500,new Error(5566,"Failed to connect to external API","https://example.com/supportexternalAPI.htm"));
```

## Alternative Designs

## Risks

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.