What should be the return type of the WEB API action method?

I am developing an ASP.NET web interface using .NET Core. This web API will be mainly accessible by the UI application (the user interface will be developed using ASP.NET Core MVC), but in the future the API may also be available by other applications.

In my WEB API, all methods are asynchronous.

If I want the client to negotiate content, what should be the return type of the API Action method Task<IActionresult> or Task<SomePOCO>

If I want the method to always return data in JSON format, then what should be the type of the returned API action method? Should it be Task<IActionresult> or Task<JsonResult> or Task<SomePOCO> , because I think all 3 will work so that it is not sure which one is suitable?

+5
source share
2 answers

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

+6
source

Take a look at the swagger: https://docs.microsoft.com/en-us/aspnet/core/tutorials/web-api-help-pages-using-swagger It is better to specify the ProducesResponseType attribute for the method:

 [ProducesResponseType(typeof(TodoItem), 201)] public IActionResult Create([FromBody, Required] TodoItem item) 

So that the automatically generated swagger documentation displays the actual returned data for the method.

+2
source

Source: https://habr.com/ru/post/1257673/


All Articles