[英]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 -> ActionResult
而ActionResult<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);
}
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.