103

In some cases I have NewtonSoft JSON.NET and in my controller I just return the JObject from my controller and all is good.

But I have a case where I get some raw JSON from another service and need to return it directly from my webAPI. In this context I can't use NewtonSoft, but if I could then I'd create a JObject from the string (which seems like unneeded processing overhead) and return that and all would be well with the world.

However, I want to return this simply, but if I return the string, then the client receives a JSON wrapper with my context as an encoded string.

How can I explicitly return a JSON from my WebAPI controller method?

7 Answers 7

219

There are a few alternatives. The simplest one is to have your method return a HttpResponseMessage, and create that response with a StringContent based on your string, something similar to the code below:

public HttpResponseMessage Get()
{
    string yourJson = GetJsonFromSomewhere();
    var response = this.Request.CreateResponse(HttpStatusCode.OK);
    response.Content = new StringContent(yourJson, Encoding.UTF8, "application/json");
    return response;
}

And checking null or empty JSON string

public HttpResponseMessage Get()
{
    string yourJson = GetJsonFromSomewhere();
    if (!string.IsNullOrEmpty(yourJson))
    {
        var response = this.Request.CreateResponse(HttpStatusCode.OK);
        response.Content = new StringContent(yourJson, Encoding.UTF8, "application/json");
        return response;
    }
    throw new HttpResponseException(HttpStatusCode.NotFound);
}
2
  • 4
    Excellent. I was making a JSON string and returning it as a string but that introduced inevitable extra " around the result. This should fix that.
    – dumbledad
    Oct 14, 2014 at 7:38
  • 1
    This is annoying. You have to actually create the HttpResponseMessage response, then assign the StringContent to the .Content property. It doesn't work if you assign the StringContent in the Constructor.
    – Suamere
    Feb 15, 2018 at 22:54
23

Here is @carlosfigueira's solution adapted to use the IHttpActionResult Interface that was introduced with WebApi2:

public IHttpActionResult Get()
{
    string yourJson = GetJsonFromSomewhere();
    if (string.IsNullOrEmpty(yourJson)){
        return NotFound();
    }
    var response = this.Request.CreateResponse(HttpStatusCode.OK);
    response.Content = new StringContent(yourJson, Encoding.UTF8, "application/json");
    return ResponseMessage(response);
}
10

This works for me in .NET Core 3.1.

private async Task<ContentResult> ChannelCosmicRaysAsync(HttpRequestMessage request)
{
    // client is HttpClient
    using var response = await client.SendAsync(request).ConfigureAwait(false); 

    var responseContentString = await response.Content.ReadAsStringAsync().ConfigureAwait(false);

    Response.StatusCode = (int)response.StatusCode;
    return Content(responseContentString, "application/json");
}
public Task<ContentResult> X()
{
    var request = new HttpRequestMessage(HttpMethod.Post, url);
    (...)

    return ChannelCosmicRaysAsync(request);
}

ContentResult is Microsoft.AspNetCore.Mvc.ContentResult.

Please note this doesn't channel headers, but in my case this is what I need.

1
  • Must have tried a dozen different ways of trying to return the raw JSON from a web request in my MVC controller. This is the only way that worked. Feb 20 at 17:02
7

sample example to return json data from web api GET method

[HttpGet]
public IActionResult Get()
{
            return Content("{\"firstName\": \"John\",  \"lastName\": \"Doe\", \"lastUpdateTimeStamp\": \"2018-07-30T18:25:43.511Z\",  \"nextUpdateTimeStamp\": \"2018-08-30T18:25:43.511Z\");
}
5
  • 4
    Where does Content come from? a fully qualified name or "using" statement would be helpful. Mar 6, 2020 at 21:41
  • I know I'm late to the party but I think @granadaCoder comment is a bit misleading. Content is a function; not a class and comes from ControllerBase same as Ok()
    – Malachy
    Jul 30, 2021 at 12:07
  • Ah, it is an ambiguous word. "Content". I'm talking about the OBJECT .. the thing after the return statement. Aka "return Content"...what is the fully qualified type name for that (upper case) "Content"? (and I'm not asking about the lower-case "content" as in.. the magic-json-string. Jul 30, 2021 at 12:10
  • Maybe I'm missing something; but the answer from Muni is calling the function Microsoft.AspNetCore.Mvc.ControllerBase.Content() which returns an object of type Microsoft.AspNetCore.Mvc.ContentResult. My point is there is no new object being returned; I do not see what could be fully qualified or clarified by adding a using statement. This is the same in my mind as return Ok(); being called to call the base class function and return an object of type Microsoft.AspNetCore.Mvc.OkResult(). I do agree that the return type is not obvious but this isn't really a flaw with the answer above
    – Malachy
    Aug 3, 2021 at 12:51
  • This worked for me and is much simpler. Adding the mimetype as the second arg to Content was needed in my case but there is probably a way to make that the default but it's so easy as is, it's not worth the time to look up how.
    – Jay13
    Mar 8, 2022 at 20:14
2

If you specifically want to return that JSON only, without using WebAPI features (like allowing XML), you can always write directly to the output. Assuming you're hosting this with ASP.NET, you have access to the Response object, so you can write it out that way as a string, then you don't need to actually return anything from your method - you've already written the response text to the output stream.

0

If your controller method returns an IActionResult you can achieve this by manually setting the output formatter.

// Alternatively, if inheriting from ControllerBase you could do
// var result = Ok(jsonAsString);
var result = new OkObjectResult(jsonAsString);

var formatter = new StringOutputFormatter();
result.Formatters.Add(formatter);

formatter.SupportedMediaTypes.Clear();
formatter.SupportedMediaTypes.Add("application/json");
-1

these also work:

[HttpGet]
[Route("RequestXXX")]
public ActionResult RequestXXX()
{
    string error = "";
    try{
        _session.RequestXXX();
    }
    catch(Exception e)
    {
        error = e.Message;
    }
    return new JsonResult(new { error=error, explanation="An error happened"});
}

[HttpGet]
[Route("RequestXXX")]
public ActionResult RequestXXX()
{
    string error = "";
    try{
        _session.RequestXXX();
    }
    catch(Exception e)
    {
        error = e.Message;
    }
    return new JsonResult(error);
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.