[英]What's the reason why Controller methods return ActionResult? (ASP.NET Core Web API)
I'm pretty new to Web APIs. 我是Web API的新手。 I observed that some methods return an
ActionResult
. 我观察到一些方法返回
ActionResult
。 Why is that? 这是为什么? Isn't it enough returning, for example, a
CustomerDto
instead? 例如,返回
CustomerDto
还不够吗?
Returning ActionResult gives no clue about the actual object being returned. 返回ActionResult不提供有关返回实际对象的任何线索。 Do I miss something?
我想念什么吗?
There are multiple ways of designing your Web API. 有多种设计Web API的方法。 You can return CustomerDto, in your case.
您可以返回CustomerDto。 According to the documentation ( https://docs.microsoft.com/en-us/aspnet/web-api/overview/getting-started-with-aspnet-web-api/action-results ), a Web API controller action can return any of the following: - void - HttpResponseMessage - IHttpActionResult - Some other type
根据文档( https://docs.microsoft.com/zh-cn/aspnet/web-api/overview/getting-started-with-aspnet-web-api/action-results),Web API控制器操作可以返回以下任何内容:-无效-HttpResponseMessage-IHttpActionResult-其他类型
By using IHttpActionResult, you can better control the Http Reponse code. 通过使用IHttpActionResult,可以更好地控制Http Reponse代码。
Web API is a flexible platform - you can write simple APIs that always just return particular result types (such as your CustomerDto
) or you can write more complex APIs that really do care about HTTP semantics and want to have more control over status codes, other HTTP headers, etc. Web API是一个灵活的平台-您可以编写简单的API,这些API始终仅返回特定的结果类型(例如
CustomerDto
),也可以编写更复杂的API,这些API确实关心HTTP语义,并希望对状态代码进行更多控制,其他HTTP标头等
Or maybe your method is dealing with "resources" which have more complex representations that aren't easily modelled by a single DTO class in C#, and so you want the flexibility of multiple possible "return types". 也许您的方法正在处理“资源”,这些资源具有更复杂的表示形式,而这些复杂的表示形式很难用C#中的单个 DTO类进行建模,因此您需要多种可能的“返回类型”的灵活性。
If you don't want that level of control, yes, you can carry on and just return your DTO types. 如果您不希望这种控制级别,可以的,您可以继续并只返回DTO类型。 Nobody is (to the best of my knowledge) forcing you to use
ActionResult
instead. 据我所知,没有人强迫您使用
ActionResult
。
In ASP.NET Core the former MVC "Controller"-class and Web Api "ApiController"-class where merged together to one "Controller"-class. 在ASP.NET Core中,以前的MVC“控制器”类和Web Api“ ApiController”类合并到一个“ Controller”类中。 The ActionResult method, which is a classic MVC method and useful when building an MVC application, where also included in that merged and that's why it's being available when also building a Web Api.
ActionResult方法是一种经典的MVC方法,在构建MVC应用程序时非常有用,它也包含在合并后的位置,这就是为什么在构建Web Api时也可以使用它的原因。
If you notice the prebuild project of the .net core web api, not all functions return ActionResult
or IActionResult
. 如果您注意到.net核心Web api的预构建项目,则并非所有函数都返回
ActionResult
或IActionResult
。 So yes, you can totally return something like CustomerDto
. 所以是的,您可以完全返回
CustomerDto
。 This way you can return an object as json, with the status code 200. However let's say your action is taking in some input and you are not always sure if there will be a valid output. 这样,您可以将状态代码为200的对象返回为json。但是,假设您的操作正在接受一些输入,并且您并不总是确定是否会有有效的输出。 In this case you will want to return status code 200 only if successful, 400 (badRequest) if the user sent invalid data or any other status code.
在这种情况下,您仅希望成功返回状态代码200,如果用户发送了无效数据或任何其他状态代码,则返回400(badRequest)。 This is the standard way of handling http requests.
这是处理http请求的标准方法。
So, by using IActionResult
you can either return a CustomerDto
object by using return Json(customer)
or return Ok(customer)
, or you can return use BadRequest(myErrors)
when you run into errors. 因此,通过使用
IActionResult
您可以使用return Json(customer)
或return Ok(customer)
返回CustomerDto
对象,或者在遇到错误时可以返回use BadRequest(myErrors)
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.