Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Return JSON object (ASP.NET WebAPI)

People also ask

How do I return JSON in Web API net core?

To return data in a specific format from a controller that inherits from the Controller base class, use the built-in helper method Json to return JSON and Content for plain text. Your action method should return either the specific result type (for instance, JsonResult ) or IActionResult .

How do I return a JSON response in C#?

ContentType = "application/json; charset=utf-8"; Response. Write(myObjectJson ); Response. End(); So you return a json object serialized with all attributes of MyCustomObject.

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.


When returning a value, try it like this:

public IHttpActionResult GetCompanies()
{
    var companies = db.Companies.ToList();
    return Ok( new { results = companies });
}