If I want the client to negotiate content , what should be the return type of the API action method?
To perform content negotiation, return Task<ObjectResult> or Task<MyPoco> .
The structure automatically wraps POCO in an ObjectResult ; therefore, both options are equivalent, and both will obey the HTTP Accept header. You will also get content matching by returning any result that implements ObjectResult , such as OkObjectResult .
If I need a method to always return data in JSON format , then what should be the type of the returned action method of the API?
To always return JSON, return Task<JsonResult> (or use the [Produces] filter).
See also: https://docs.asp.net/en/latest/mvc/models/formatting.html#content-negotiation
[S] o I assume that then IActionResult used only for the MVC controller?
IActionResult is a contract for all results returned by a Controller . If your action signature is of type return type IActionResult , then the body of your action can return any type of result, because they all implement the IActionResult interface. Here is the inheritance hierarchy.
IActionResult ActionResult ChallengeResult ContentResult EmptyResult FileResult FileContentResult FileStreamResult PhysicalFileResult VirtualFileResult ForbidResult LocalRedirectResult ObjectResult CreatedAtActionResult CreatedAtRouteResult CreatedResult BadRequestObjectResult NotFoundObjectResult OkObjectResult RedirectResult RedirectToActionResult RedirectToRouteResult SignInResult SignOutResult StatusCodeResult NoContentResult NotFoundResult OkResult UnauthorizedResult UnsupportedMediaTypeResult BadRequestResult
See also: https://github.com/aspnet/Mvc/tree/dev/src/Microsoft.AspNetCore.Mvc.Core
source share