简体   繁体   English

ASP.net 核心 API 返回 ActionResult<t> 不强制返回类型</t>

[英]ASP.net Core API returning ActionResult<T> not enforcing return type

Where an API is returning an ActionResult<T> there is no compile or run-time checks to ensure that the data type being returned is as declared.如果 API 返回ActionResult<T> ,则不会进行编译或运行时检查以确保返回的数据类型与声明的相同。

For example:例如:

public ActionResult<Dog> GetCat()
{
    var cat = new Cat("Moggy");
    return Ok(cat);
}

This compiles, runs and returns a Cat object.这将编译、运行并返回 Cat object。

The problem I have is that the OpenAPI defintion (Swagger/Swashbuckle) is being derived from the type declared on the method.我遇到的问题是 OpenAPI 定义(Swagger/Swashbuckle)是从方法上声明的类型派生的。 This means the OpenAPI defintion can be out of sync with the code and there is no obvious way to find these instances.这意味着 OpenAPI 定义可能与代码不同步,并且没有明显的方法可以找到这些实例。

In the example given, my API documentation would tell people that this API returns a Dog when really it returns a Cat.在给出的示例中,我的 API 文档会告诉人们,这个 API 返回的是一只狗,而实际上它返回的是一只猫。

Is there any way to enforce the return type at compile time, or failing that at run-time?有没有办法在编译时强制执行返回类型,或者在运行时失败?

That is because your returned type OkObjectResult inherits the ActionResult .那是因为您返回的类型OkObjectResult继承了ActionResult The returned child class does not impact the Parent class compilation.This is by design.返回的子 class 不会影响父 class 编译。这是设计使然。

More details about the two class you could refer to the following source code:有关这两个 class 的更多详细信息,您可以参考以下源代码:

1.The Ok method returns type of OkObjectResult : 1. Ok方法返回OkObjectResult的类型:

// Summary:
//     A base class for an MVC controller without view support.
[Controller]
public abstract class ControllerBase
{       
      // Summary:
    //     Creates an Microsoft.AspNetCore.Mvc.OkObjectResult object that produces an Microsoft.AspNetCore.Http.StatusCodes.Status200OK
    //     response.
    //
    // Parameters:
    //   value:
    //     The content value to format in the entity body.
    //
    // Returns:
    //     The created Microsoft.AspNetCore.Mvc.OkObjectResult for the response.
    [NonAction]
    public virtual OkObjectResult Ok([ActionResultObjectValueAttribute] object value);

2.The OkObjectResult inherits ObjectResult : 2. OkObjectResult继承ObjectResult

    //
// Summary:
//     An Microsoft.AspNetCore.Mvc.ObjectResult that when executed performs content
//     negotiation, formats the entity body, and will produce a Microsoft.AspNetCore.Http.StatusCodes.Status200OK
//     response if negotiation and formatting succeed.
[DefaultStatusCode(200)]
public class OkObjectResult : ObjectResult
{
    //
    // Summary:
    //     Initializes a new instance of the Microsoft.AspNetCore.Mvc.OkObjectResult class.
    //
    // Parameters:
    //   value:
    //     The content to format into the entity body.
    public OkObjectResult(object value);
}

3.The ObjectResult inherits ActionResult : ObjectResult继承ActionResult

public class ObjectResult : ActionResult, IStatusCodeActionResult, IActionResult    
{
   public ObjectResult(object value);
   //..
}

Is there any way to enforce the return type at compile time, or failing that at run-time?有没有办法在编译时强制执行返回类型,或者在运行时失败?

ActionResult<T> enables you to return a type deriving from ActionResult or return a specific type. ActionResult<T>使您能够返回派生自ActionResult的类型或返回特定类型。

To enforce compile error,you could just return cat which is a specific type.And you could see the error message disappear when you change Dog to Cat :要强制编译错误,您可以只返回特定类型的 cat。当您将Dog更改为Cat时,您会看到错误消息消失:

public ActionResult<Dog> GetCat()
{
    var cat = new Cat("Moggy");
    return cat;
}

Result:结果:

在此处输入图像描述

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 ASP.NET Core如何将任何类型转换为ActionResult <T> 控制器动作的返回类型? - How is ASP.NET Core able to convert any type to ActionResult<T> return type of controller actions? 单元测试中的 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 <IEnumerable<ZapResult> &gt;` 返回任何对象的 `BadRequest()`? - Why can an ASP.NET Core action returning an ` ActionResult<IEnumerable<ZapResult>>` return a `BadRequest()` of any object? Controller方法返回ActionResult的原因是什么? (ASP.NET Core Web API) - What's the reason why Controller methods return ActionResult? (ASP.NET Core Web API) ASP.NET Core API - ActionResult<T> vs 异步任务<T> - ASP.NET Core API - ActionResult<T> vs async Task<T> 转换 ASP.NET Core ActionResult<T> 对象到过滤器中的另一种类型 - Convert ASP.NET Core ActionResult<T> object to another type in a filter API 在 ASP.NET 核心中返回不受支持的媒体类型 - API returning unsupported media type in ASP.NET Core asp.net mvc return actionresult for video - asp.net mvc return actionresult for video 在asp.net核心api中返回集合 - return collection in asp.net core api 使用ASP.NET Core API返回视图 - Return a view with ASP.NET Core API
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM