I have the following controller and action.
[Route("/api/simple")]
public class SimpleController : Controller
{
[HttpGet]
[Route("test")]
public string Test()
{
return "test";
}
}
When I call it, I expect action to return "test"
(which is valid JSON), but instead it returns test
(without quotation marks) is this a valid behavior, or bug? Am I missing something?
GET http://localhost:5793/api/simple/test HTTP/1.1
User-Agent: Fiddler
Host: localhost:5793
Accept: application/json
HTTP/1.1 200 OK
Content-Type: text/plain; charset=utf-8
Server: Microsoft-IIS/10.0
X-Powered-By: ASP.NET
Date: Sun, 09 Aug 2015 14:37:45 GMT
Content-Length: 4
test
It seems that the StringOutputFormatter is getting into your way. If you remove it or move it after the JsonOutputFormatter you will get the desired results.
As @mbudnik pointed out, the culprit here is StringOutputFormatter, which somehow gets selected to format the output instead of JsonOutputFormatter. His code snippet, however, no longer works because there’s been some changes to ASP.NET Core since then. Use this instead:
Or, if you think you don't need StringOutputFormatter at all, you can remove it altogether:
IMO this should be considered a bug, since you asked for a JSON response (
Accept: application/json
) and returning the string without quotes is definitely not JSON. However, the official position is that this is expected.