dotnet / dotnet/aspnetcore

Route handler returns 200 but the response body is empty

Open
#67,089 5 comments 0 reactions 1 assignee Claimed by @Youssef1313 View on GitHub
area-minimal
Dominant language
C#
Stars
38.4k
Forks
10.9k
Avg merge
2d 5h
Merged PRs (30d)
276

Description

### Is there an existing issue for this?

- [x] I have searched the existing issues

### Describe the bug

Preconditions:
1. using asp.net core 10 minimal api
2. using external IoC container SimpleInjector together with asp.net core's dependency injection
3. need to pass HttpContext to a route handler, using [FromService]
4. route handler object is resolved by SimpleInjector
5. route handler returns TypedResults

Problem:
Route handler returns 200 but the response body is empty.
See github repo.

```c#
// this results in 200 but empty response
app.MapGet("/product", async (
HttpContext context
) => await container.GetInstance().ExecuteAsync(context));
```

But if I enclose the lambda body with curly braces, it works:
```c#
// with code block but same everything else: response is populated
app.MapGet("/succcessfulproduct", async (
HttpContext context
) =>
{
var result = await container.GetInstance().ExecuteAsync(context);
return result;
});
```

Also, if I don't use HttpContext, it works:
```c#
// other dependencies are not producing this issue
app.MapGet("/withouthttpcontext", async (
HttpClient httpClient
) => await container.GetInstance().WithoutHttpContext(httpClient));
```

### Expected Behavior

Route handler should behave the same whether I enclose the lambda body with curly braces or not.

### Steps To Reproduce

https://github.com/ndc/MinimalApiWithSimpleInjector

See comments in `EndpointRegistration`

### Exceptions (if any)

_No response_

### .NET Version

10.0.300

### Anything else?

_No response_

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.