Suggest the use of the Results class to produce responses instead of manually writing to the HttpResponse
- Dominant language
- C#
- Stars
- 38.4k
- Forks
- 10.9k
- Avg merge
- 2d 10h
- Merged PRs (30d)
- 281
Description
This sample:
```C#
app.MapGet("/todos/{id}", (int id, HttpContext context) =>
{
var todo = db.Find(id);
if (todo is null)
{
context.Response.StatusCode = 400;
return Task.CompletedTask;
}
return context.Response.WriteAsJsonAsync(todo);
});
```
Gets changed to this:
```C#
app.MapGet("/todos/{id}", (int id) =>
{
var todo = db.Find(id);
if (todo is null)
{
return Results.BadRequest();
}
return Results.Ok(todo);
});
```
This goes for setting the content type as well:
```C#
app.MapGet("/content", (HttpContext context) =>
{
var content = @"top";
context.Response.ContentType = "application/xml";
return context.Response.WriteAsync(content);
});
```
Suggested to:
```C#
app.MapGet("/content", () =>
{
var content = @"top";
return Results.Text(content, "application/xml");
});
```
Contributor guide
Assessment
This issue has not been assessed yet.