Return "raw" json in ASP.NET Core 2.0 Web Api
Asked Answered
L

4

76

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.

Lactoflavin answered 19/1, 2018 at 8:12 Comment(2)
json sent as the body or a param?Systole
@NevilleNazerane JSON sent as the return value (body), so no params involved.Lactoflavin
L
121

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");
Lactoflavin answered 19/1, 2018 at 8:34 Comment(5)
Better yet to return Json(your json goes here) as per learn.microsoft.com/en-us/aspnet/core/web-api/advanced/…Mlle
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.Coif
How do you do this for DotNetCore 2.1 ? The "Content" method is not recognized - so I believe they've changed something since.Indeterminism
@Indeterminism You need ASPNetCore 2.1. NetCore2.1 is not enough. Content is a function of Microsoft.AspNetCore.Mvc.ControllerBaseLactoflavin
You may consider using MediaTypeNames.Application.Json instead of "application/json" (for .NET Core 2.1+).Thynne
P
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

Paniculate answered 19/1, 2018 at 8:34 Comment(3)
This is great. I have a feeling that this changed between ASP.NET Core 2 and ASP.NET Core 1.Stall
another day of hair pulling here too. Would love to see a working solution for DNCore 2.1!Indeterminism
@Indeterminism You need ASPNetCore 2.1. NetCore2.1 is not enough. Content is a function of Microsoft.AspNetCore.Mvc.ControllerBaseLactoflavin
R
5

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

return new JsonResult(json);
Rebecarebecca answered 29/12, 2020 at 8:22 Comment(2)
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 frameworkWoadwaxen
in net6.0 This also escapes the string parameter, so does not really do what the OP wants.Afloat
A
0

this works for me:

return Content(json, MediaTypeNames.Application.Json);
Afloat answered 5/5 at 9:36 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.