77

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.

2
  • json sent as the body or a param? Commented Jan 19, 2018 at 8:24
  • @NevilleNazerane JSON sent as the return value (body), so no params involved. Commented Jan 19, 2018 at 9:27

4 Answers 4

122

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");
Sign up to request clarification or add additional context in comments.

5 Comments

Better yet to return Json(your json goes here) as per learn.microsoft.com/en-us/aspnet/core/web-api/advanced/…
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.
How do you do this for DotNetCore 2.1 ? The "Content" method is not recognized - so I believe they've changed something since.
@MC9000 You need ASPNetCore 2.1. NetCore2.1 is not enough. Content is a function of Microsoft.AspNetCore.Mvc.ControllerBase
You may consider using MediaTypeNames.Application.Json instead of "application/json" (for .NET Core 2.1+).
30

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://learn.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.mvc.contentresult?view=aspnetcore-2.0

3 Comments

This is great. I have a feeling that this changed between ASP.NET Core 2 and ASP.NET Core 1.
another day of hair pulling here too. Would love to see a working solution for DNCore 2.1!
@MC9000 You need ASPNetCore 2.1. NetCore2.1 is not enough. Content is a function of Microsoft.AspNetCore.Mvc.ControllerBase
4

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

return new JsonResult(json);

2 Comments

With the release of .NET Core 3.0 - as <TargetFramework>netcoreapp3.1</TargetFramework> - we can now use JsonResult as a cleaner solution, and it does not require any Nuget packages as it is part of the Microsoft.AspNetCore.App framework
in net6.0 This also escapes the string parameter, so does not really do what the OP wants.
0

this works for me:

return Content(json, MediaTypeNames.Application.Json);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.