简体   繁体   English

Controller方法返回ActionResult的原因是什么? (ASP.NET Core Web API)

[英]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的预构建项目,则并非所有函数都返回ActionResultIActionResult 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.

相关问题 Asp.net 5 web api 自定义操作结果 - Asp.net 5 web api Custom ActionResult 单元测试中的 ActionResult 问题无法在 POST 返回值上 Assert.Equal (asp.net core mvc web api) - Trouble with ActionResult in unit test can't Assert.Equal on POST return value (asp.net core mvc web api) ASP.NET Core如何将任何类型转换为ActionResult <T> 控制器动作的返回类型? - How is ASP.NET Core able to convert any type to ActionResult<T> return type of controller actions? C# ASP.NET Core Web API:如何在controller调用不同方法之间保持上下文 - C# ASP.NET Core Web API: How to maintain context in the controller between calling different methods ASP.net 核心 API 返回 ActionResult<t> 不强制返回类型</t> - ASP.net Core API returning ActionResult<T> not enforcing return type 为什么 ASP.NET Core 操作可以返回 `ActionResult <IEnumerable<ZapResult> &gt;` 返回任何对象的 `BadRequest()`? - Why can an ASP.NET Core action returning an ` ActionResult<IEnumerable<ZapResult>>` return a `BadRequest()` of any object? ASP.Net Core 3.1 WEB API - Controller 方法不返回正确的 Z0ECD11C1D7A287401F814A8 - ASP.Net Core 3.1 WEB API - Controller method doesn't return correct JSON 如何从 ASP.Net Core Web API Controller 中的数据库返回关系数据 - How to return a relational data from DB in ASP.Net Core Web API Controller 单个controller ASP.NET Web API中有多个GET方法 - Single controller with multiple GET methods in ASP.NET Web API 如何从控制器进行 HTTP 调用? 使用 Web API 的 Asp.Net Core C# - How to make HTTP call from Controller ? to Use web API's Asp.Net Core C#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM