Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Return a JSON string explicitly from Asp.net WEBAPI?

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 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?

like image 328
klumsy Avatar asked Jun 13 '13 21:06

klumsy


People also ask

How do I return data from Web API?

Learn the three ways you can return data from your ASP.NET Core Web API action methods. We have three ways to return data and HTTP status codes from an action method in ASP.NET Core. You can return a specific type, return an instance of type IActionResult, or return an instance of type ActionResult.

How do I return a JSON response?

To return JSON from the server, you must include the JSON data in the body of the HTTP response message and provide a "Content-Type: application/json" response header. The Content-Type response header allows the client to interpret the data in the response body correctly.

Which of the following Web API method is used to update data?

Here, we will implement PUT method in the Web API. The HTTP PUT method is used to update an existing record in the data source in the RESTful architecture.


2 Answers

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); } 
like image 128
carlosfigueira Avatar answered Sep 24 '22 10:09

carlosfigueira


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); } 
like image 22
Jpsy Avatar answered Sep 26 '22 10:09

Jpsy