简体   繁体   English

ASP.NET Core如何将任何类型转换为ActionResult <T> 控制器动作的返回类型?

[英]How is ASP.NET Core able to convert any type to ActionResult<T> return type of controller actions?

I have simple action in WebApi controller in ASP.NET Core 2.2 that looks like this: 我在ASP.NET Core 2.2中的WebApi控制器中有一个简单的操作,如下所示:

[HttpGet("test123")]
public ActionResult<string> Test123()
{
    return new OkResult();
}

This compiles fine but I am wondering how is it possible that OkResult object is converted to ActionResult<string> ? 这样编译可以,但是我想知道OkResult对象怎么可能转换为ActionResult<string>

These classes have different inheritance chain: OkResult -> StatusCodeResult -> ActionResult while ActionResult<TValue> only implements IConvertToActionResult In other words, ActionResult<string> is not base type for OkResult class. 这些类具有不同的继承链: OkResult -> StatusCodeResult -> ActionResultActionResult<TValue>仅实现IConvertToActionResult 。换句话说, ActionResult<string>不是OkResult类的基本类型。

If I do that manually and change code to: 如果我手动执行此操作并将代码更改为:

[HttpGet("test123")]
public ActionResult<string> Test123()
{
    var a = new OkResult();
    var b = a as ActionResult<string>;  // Error CS0039

    return b;
}

the code wont compile with the conversion error: 该代码将不会编译并显示转换错误:

Error CS0039: Cannot convert type 'Microsoft.AspNetCore.Mvc.OkResult' to 'Microsoft.AspNetCore.Mvc.ActionResult' via a reference conversion, boxing conversion, unboxing conversion, wrapping conversion, or null type conversion 错误CS0039:无法通过引用转换,装箱转换,拆箱转换,换行转换或空类型转换将类型“ Microsoft.AspNetCore.Mvc.OkResult”转换为“ Microsoft.AspNetCore.Mvc.ActionResult”

How is it possible that first code works while the second doesn't? 第一个代码如何工作而第二个代码却不工作呢? How is return type converted from objects that don't have common base type? 如何从没有通用基本类型的对象转换返回类型?

The following two implicit operators from ActionResult<TValue> ActionResult<TValue>的以下两个隐式运算符

/// <summary>
/// Implictly converts the specified <paramref name="value"/> to an <see cref="ActionResult{TValue}"/>.
/// </summary>
/// <param name="value">The value to convert.</param>
public static implicit operator ActionResult<TValue>(TValue value)
{
    return new ActionResult<TValue>(value);
}

/// <summary>
/// Implictly converts the specified <paramref name="result"/> to an <see cref="ActionResult{TValue}"/>.
/// </summary>
/// <param name="result">The <see cref="ActionResult"/>.</param>
public static implicit operator ActionResult<TValue>(ActionResult result)
{
    return new ActionResult<TValue>(result);
}

Source 资源

Is what allows for the use of multiple return types in the action. 是什么允许在操作中使用多种返回类型。

[HttpGet("test123")]
public ActionResult<string> Test123() {
    if(someCondition) return "String value"; //<--String
    return Ok(); //<-- OkResult
}

When the string is returned the ActionResult<TValue>(TValue value) operator is invoked, returning a valid ActionResult<TValue> and vice versa of the other operator. 返回字符串后,将调用ActionResult<TValue>(TValue value)运算符,并返回有效的ActionResult<TValue> ,反之亦然。

Your first example takes advantage of an implicit user-defined type conversion operator, which looks like this : 您的第一个示例利用了隐式的用户定义类型转换运算符,该运算符如下所示

public static implicit operator ActionResult<TValue>(ActionResult result)
{
    return new ActionResult<TValue>(result);
}

Your second example, using as , is unable to use the implicit conversion operator as, according to the docs , it: 根据docs ,您的第二个示例as使用不能使用隐式转换运算符as:

...performs only reference conversions, nullable conversions, and boxing conversions. ...仅执行参考转换,可为空的转换和装箱转换。 The as operator can't perform other conversions, such as user-defined conversions, which should instead be performed by using cast expressions. as运算符不能执行其他转换,例如用户定义的转换,而应使用强制转换表达式来执行。

暂无
暂无

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

相关问题 转换 ASP.NET Core ActionResult<T> 对象到过滤器中的另一种类型 - Convert ASP.NET Core ActionResult<T> object to another type in a filter ASP.net 核心 API 返回 ActionResult<t> 不强制返回类型</t> - ASP.net Core API returning ActionResult<T> not enforcing return type ASP.NET Core 6“无法将类型'Rotativa.ViewAsPdf'隐式转换为'Microsoft.AspNetCore.Mvc.ActionResult'” - ASP.NET Core 6 "Cannot implicitly convert type 'Rotativa.ViewAsPdf' to 'Microsoft.AspNetCore.Mvc.ActionResult'" ASP.NET Core API如何在操作方法中将ActionResult <T>转换为T. - How ASP.NET Core APIs convert ActionResult<T> to T in action methods Controller方法返回ActionResult的原因是什么? (ASP.NET Core Web API) - What's the reason why Controller methods return ActionResult? (ASP.NET Core 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? 任务的返回类型<type>在 ASP.NET 核心控制器中</type> - Return Type from Task<Type> in ASP.NET CORE Controllers ASP.NET Core - 从 controller 类型获取路由 - ASP.NET Core - get route from controller type 如何在asp.net core中编写自定义actionResult - How to write custom actionResult in asp.net core 如何在 ASP.NET 核心中使用 mysql 进行单元测试和操作结果 - How to unit test, actionresult with using mysql in ASP.NET Core
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM