-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Controller POST action returning simple string does not respect the request Accept header for text/plain? #7811
Comments
If you have text that you want to return verbatim from an API, then I would recommend using The rationale is that if you already have text, and you just want to return it, you've already made a decision about what the content type is when you encoded it. |
@rynowak . The design of the Web API (ASP.NET or .NET Core) is to enable application developers to write API codes rather than "return Content(...)" which is a clumsy workaround. I would expect the .NET Core team to fix this, so to be consistent with the behavior of the GET action. |
Can you please provide a repo application that demonstrates the problem? The behavior you describe for your code sample is intended. If you return an object from an action method, it will be serialized by formatters, in this case the JSON formatter. |
@rynowak, this point is, as mentioned in the OP, with a GET action, the response is plain text by default, not a JSON string object. And this is apparently by design, as suggested by the .NET Core team at #4945 Ah, I have just realized that you @rynowak were the guy who had answered in the other thread. Do you agree that GET and POST actions should consistently return plain text by default when the function prototype has the return type is string? If yes, I shall provide a simple repo application that demonstrates the problem. |
Just found out in my client somewhere, I had the accept header set to application/json first, so .net core just responds according to the first accept header. So it is confirmed that GET and POST actions should consistently return plain text by default . Thanks. Case closed. |
Bug:
While the GET action respects the request Accept header for text/plain or application/json, the POST action always return JSON data which is a string quoted by double quotes, regardless of the Accept header.
Steps to reproduce :
Controller Action with ControllerBase or Controller:
Client:
Result:
Description of the problem:
If the client want string, it is good just to give string, rather than JSON string with double quotes.
Version of
Microsoft.AspNetCore.All
: 2.0.6The text was updated successfully, but these errors were encountered: