53

The standard way AFAIK to return data in ASP.NET Core Web Api is by using IActionResult and providing e.g. an OkObject result. This works fine with objects, but what if I have obtained a JSON string somehow, and I just want to return that JSON back to the caller?

e.g.

public IActionResult GetSomeJSON()
{
    return Ok("{ \"name\":\"John\", \"age\":31, \"city\":\"New York\" }");
}

What ASP.NET Core does here is, it takes the JSON String, and wraps it into JSON again (e.g. it escapes the JSON)

Returning plain text with [Produces("text/plain")] does work by providing the "RAW" content, but it also sets the content-type of the response to PLAIN instead of JSON. We use [Produces("application/json")] on our Controllers.

How can I return the JSON that I have as a normal JSON content-type without it being escaped?

Note: It doesn't matter how the JSON string was aquired, it could be from a 3rd party service, or there are some special serialization needs so that we want to do custom serialization instead of using the default JSON.NET serializer.

MichelZ
  • 3,456
  • 4
  • 25
  • 33

3 Answers3

91

And of course a few minutes after posting the question I stumble upon a solution :)

Just return Content with the content type application/json...

return Content("{ \"name\":\"John\", \"age\":31, \"city\":\"New York\" }", "application/json");
MichelZ
  • 3,456
  • 4
  • 25
  • 33
  • 3
    Better yet to return Json(your json goes here) as per https://docs.microsoft.com/en-us/aspnet/core/web-api/advanced/formatting?view=aspnetcore-2.0 – Nickolodeon Nov 21 '18 at 08:25
  • 1
    This was hugely helpful - thank you! (If i return Json as per the above comment, then it returns it chunked which my client can't use.) Your solution of returning Content with a specification of application/json is what solved my problem. – bab Jan 07 '19 at 21:43
  • How do you do this for DotNetCore 2.1 ? The "Content" method is not recognized - so I believe they've changed something since. – MC9000 Feb 28 '19 at 21:43
  • @MC9000 You need ASPNetCore 2.1. NetCore2.1 is not enough. `Content` is a function of `Microsoft.AspNetCore.Mvc.ControllerBase` – MichelZ Mar 01 '19 at 09:16
  • You may consider using [`MediaTypeNames.Application.Json`](https://stackoverflow.com/a/54650792) instead of `"application/json"` (for .NET Core 2.1+). – Pang May 15 '21 at 09:38
28

In your action, replace Ok() with the Content() method, which lets you set the content (raw content), content type, and status code of your response: https://docs.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.mvc.contentresult?view=aspnetcore-2.0

Arash Motamedi
  • 5,589
  • 3
  • 27
  • 34
  • 1
    This is great. I have a feeling that this changed between ASP.NET Core 2 and ASP.NET Core 1. – James Woodall Jun 02 '18 at 16:20
  • another day of hair pulling here too. Would love to see a working solution for DNCore 2.1! – MC9000 Feb 28 '19 at 21:44
  • @MC9000 You need ASPNetCore 2.1. NetCore2.1 is not enough. `Content` is a function of `Microsoft.AspNetCore.Mvc.ControllerBase` – MichelZ Mar 01 '19 at 09:16
1

This worked for me, where Json() did not:

return new JsonResult(json);
Uwe Keim
  • 36,867
  • 50
  • 163
  • 268
Oliver Bock
  • 4,185
  • 3
  • 33
  • 56