HttpContext.Response.Body.Position = 0 - "Specified method is not supported" error

I was able to solve this:

Firstly, I set the response to its own memory stream and call await _next(context) after the stream was set:

var responseBodyStream = new MemoryStream();
context.Response.Body = responseBodyStream;

await _next(context);

Then once I did this, I noticed I was getting an empty body back, this was due to trying to set an empty body back as the response context:

await responseBodyStream.CopyToAsync(context.Response.Body);

I removed this line and everything started working correctly.


I was facing this issue in my Asp.Net core API today.

enter image description here

The issue was, I forgot to add the [FromBody] parameter to my API. After adding the same as below, the issue was resolved.

[HttpPost("merkliste/create")]
public virtual async Task<IActionResult> MerklisteWorksheetCreate(string worksheetName, [FromBody] string elementDetailsArray)

enter image description here

Hope it helps.