Prevent chunking in ASP.NET Core 6 Web API

1 day ago 2
ARTICLE AD BOX

I have a simple ASP.NET Core 6 Web API. I am receiving a POST request and sending back a JSON response in the body.

In newer versions, I can use:

var updatedJsonObject = JsonConvert.SerializeObject(status, new JsonSerializerSettings { ContractResolver = new CamelCasePropertyNamesContractResolver() }); string response = updatedJsonObject.ToString(); return Results.Text(response, "application/json");

And it sets the Content-Length header properly and doesn't send back a chunked response. I can manually set the Content-Length header, but it is always wrong because the .NET system is automatically adding to the body of the response:

"contentType":"application/json","statusCode":null

So, it is basically wrapping my JSON object in a wrapper and adding it's own properties.

Here is what I should get:

{\"status\":\"OK\"}

But here is what I am actually getting:

{"content":"{\"status\":\"OK\"}","contentType":"application/json","statusCode":null}

Two questions:

How can I return the raw JSON string that has already been created as the body without any additions from .NET? How can I prevent the API from sending a chunked response?

I have this working perfectly in an ASP.NET Core 6 project, but I am trying to update a legacy project that is using .NET 6 and I can't upgrade it.

The Web API is using Kestrel with a signature:

[HttpPost] public async Task<IResult> Post()

I have tried using Results.Content, Results.Ok, Results.Json, etc. They all wrap the response body in their own JSON properties.

It seems like such a simple thing, but is driving me crazy. Any help would be greatly appreciated.

Read Entire Article