WEB API操作方法的返回类型应该是什么? [英] What should be the return type of WEB API Action Method?
问题描述
我正在使用.NET Core开发ASP.NET Web API.该Web API将主要由UI应用程序访问(UI将使用ASP.NET Core MVC开发),但将来该API也可能会被其他应用程序访问.
I am developing ASP.NET Web API using .NET Core. This Web API is going to be mainly accessed by UI application (UI will be developed using ASP.NET Core MVC) but in future API may be accessed by other applications as well.
在我的WEB API中,所有方法都是异步的.
In my WEB API all methods are async.
如果我希望客户端进行内容协商,那么API操作方法Task<IActionresult>
或Task<SomePOCO>
If I want client to do content negotiation then what should be the return type of the API action method Task<IActionresult>
or Task<SomePOCO>
如果我希望方法始终以JSON格式返回数据,那么API操作方法的返回类型应该是什么?应该是Task<IActionResult>
还是Task<JsonResult>
或Task<SomePOCO>
,因为我认为这3个都可以工作,所以不确定在这里哪一个合适?
If I want method to always return data in JSON format then what should be return type of the API action method? Should it be Task<IActionResult>
or Task<JsonResult>
or Task<SomePOCO>
because I think all 3 would work so not sure which one is appropriate here?
推荐答案
如果我希望客户端进行内容协商,那么API操作方法的返回类型应该是什么?
If I want [the] client to do content negotiation then what should be the return type of the API action method?
要进行内容协商,请返回Task<ObjectResult>
或Task<MyPoco>
.
To do content negotiation, return Task<ObjectResult>
or Task<MyPoco>
.
该框架自动将POCO包装在ObjectResult
中;因此,这两个选项是等效的,并且都将遵循HTTP Accept
标头.您还可以通过返回实现ObjectResult
的任何结果(例如OkObjectResult
所做的结果)来进行内容协商.
The framework automatically wraps POCOs in an ObjectResult
; therefor, both options are equivalent, and both will obey the HTTP Accept
header. You will also get content negotiation by returning any result that implements ObjectResult
, such as an OkObjectResult
does.
如果我希望[strong]方法始终以JSON格式返回数据,那么API操作方法的[返回类型]应该是什么?
If I want [the] method to always return data in JSON format then what should be [the] return type of the API action method?
要始终返回JSON,请返回Task<JsonResult>
(或使用[Produces]
过滤器).
To always return JSON, return a Task<JsonResult>
(or use the [Produces]
filter).
另请参见: https://docs.asp .net/zh-CN/latest/mvc/models/formatting.html#content-negotiation
[S] o我假设
IActionResult
仅用于MVC控制器?
[S]o I am assuming then
IActionResult
is only used for MVC controller?
IActionResult
是Controller
返回的 all 结果的合同.如果您的操作的签名具有IActionResult
返回类型,则您的操作的方法主体可以return
任何结果类型,因为它们都实现了IActionResult
接口.这是继承层次结构.
IActionResult
is the contract for all results that a Controller
returns. If your action's signature has an IActionResult
return type, then your action's method body can return
any result type, because all of them 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
另请参阅: https://github.com/aspnet/Mvc/tree/dev/src/Microsoft.AspNetCore.Mvc.Core
这篇关于WEB API操作方法的返回类型应该是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!