Getting plaintext response from ASP.NET Core 7 Web API endpoint

409 Views Asked by At

I have a simple ASP.NET Core 7 Web API method:

[HttpPost("processPiece")]
public async Task<ActionResult<string>> ProcessPiece([FromBody] PieceModel piece)
{
    return _processingService.ProcessPiece(piece.Piece);
} 

The ProcessPiece method returns a string value. It contains multiple lines.

I'm trying to display this value in a Blazor component on the UI.

_httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
var response = await _httpClient.PostAsync($"api/Piece/processPiece", new StringContent("{\"piece\":\"" + piece + "\"}", Encoding.UTF8, "application/json"));
return await response.Content.ReadAsStringAsync();

The view markup is very simple for now:

<p>@outputValue</p>

I found out that response.Content.ReadAsStringAsync(); returns the string wrapped in extra quotes, and instead of rendering a new line it shows "\r\n".

It seems the output is somehow escaped during transport or decoding.

I've looked for solutions here, but all the threads I found on the topic seem to be at least 10 years old and the provided solutions don't seem to work.

Regardless, I've tried implementing one of the suggested solutions, by switching the type the endpoint returns to plaintext:

[HttpPost("processPiece")]
public async Task<ActionResult<HttpResponseMessage>> ProcessPiece([FromBody] PieceModel piece)
{
     var  a =  _processingService.ProcessPiece(piece.Piece);
     var resp = new HttpResponseMessage(HttpStatusCode.OK);
     resp.Content = new StringContent(a, System.Text.Encoding.UTF8, "text/plain");
     return resp;
}

Client side:

_httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("text/plain"));
    var response = await _httpClient.PostAsync($"api/Piece/processPiece", new StringContent("{\"piece\":\"" + piece + "\"}", Encoding.UTF8, "application/plaintext"));
    var a = await response.Content.ReadAsStringAsync();
    return a;

This didn't work at all. Now all I get from the endpoint is a bunch of metadata:

{"version":"1.1","content":{"headers":[{"key":"Content-Type","value":["text/plain; charset=utf-8"]}]},"statusCode":200,"reasonPhrase":"OK","headers":[],"trailingHeaders":[],"requestMessage":null,"isSuccessStatusCode":true}

Can anyone help?

1

There are 1 best solutions below

3
On

ASP.NET Core does not handle HttpResponseMessage as a special type, so you result will be serialized to JSON (so if you return just a string it will be encoded as JSON, hence the extra quotes). Just use one of the Content options. For example:

public async Task<IActionResult> ProcessPiece([FromBody] PieceModel piece)
{
    return Content("somestring", "text/plain", System.Text.Encoding.UTF8);
}

Or via Results/TypedResults (depending on the use case).