To implement response type JSON in a .NET Core API, you can use the JsonResult class. The
JsonResult class is a type of ActionResult that can be used to return JSON data from an API endpoint.
To use the JsonResult class, you need to first install the
Microsoft.AspNetCore.Mvc.NewtonsoftJson NuGet package. This package provides the Newtonsoft.Json library, which is used to serialize and deserialize JSON data.
Once you have installed the NuGet package, you can use the JsonResult class in your API endpoints. For example, the following code returns a JSON object with a single property named "message":
C#
public class MessageController : Controller
{
[HttpGet("/message")]
public JsonResult GetMessage()
{
return Json(new { message = "Hello, world!" });
}
}
In this example, the GetMessage() action returns a JsonResult object with a single property named "message". The value of the "message" property is a string literal that contains the text "Hello, world!".
When the GetMessage() action is called, the Newtonsoft.Json library will serialize the JSON object and write it to the response stream. The response content type will be set to
application/json.
You can also use the JsonResult class to return JSON objects that contain complex data structures. For example, the following code returns a JSON object that contains an array of numbers:
C#
public class NumberController : Controller
{
[HttpGet("/numbers")]
public JsonResult GetNumbers()
{
return Json(new[] { 1, 2, 3, 4, 5 });
}
}
In this example, the GetNumbers() action returns a JsonResult object with an array of numbers. The array contains the numbers 1, 2, 3, 4, and 5.
The JsonResult class is a powerful tool that can be used to return JSON data from .NET Core APIs. By using the
JsonResult class, you can easily and efficiently return JSON data from your APIs.
Markdown for AI
A clean, structured version of this page for AI assistants and LLMs.
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that you have read and understood our
Cookie Policy &
Privacy Policy.
To implement response type JSON in a .NET Core API, you can use the
JsonResultclass. TheJsonResultclass is a type ofActionResultthat can be used to return JSON data from an API endpoint.To use the
JsonResultclass, you need to first install theMicrosoft.AspNetCore.Mvc.NewtonsoftJsonNuGet package. This package provides the Newtonsoft.Json library, which is used to serialize and deserialize JSON data.Once you have installed the NuGet package, you can use the
JsonResultclass in your API endpoints. For example, the following code returns a JSON object with a single property named "message":C#
In this example, the
GetMessage()action returns aJsonResultobject with a single property named "message". The value of the "message" property is a string literal that contains the text "Hello, world!".When the
GetMessage()action is called, the Newtonsoft.Json library will serialize the JSON object and write it to the response stream. The response content type will be set toapplication/json.You can also use the
JsonResultclass to return JSON objects that contain complex data structures. For example, the following code returns a JSON object that contains an array of numbers:C#
In this example, the
GetNumbers()action returns aJsonResultobject with an array of numbers. The array contains the numbers 1, 2, 3, 4, and 5.The
JsonResultclass is a powerful tool that can be used to return JSON data from .NET Core APIs. By using theJsonResultclass, you can easily and efficiently return JSON data from your APIs.