一、IActionResult 是什么?
所在命名空间:
using Microsoft.AspNetCore.Mvc;
接口源码的核心定义如下:
public interface IActionResult
{
Task ExecuteResultAsync(ActionContext context);
}
它的作用是统一描述不同类型的 HTTP 响应,包括:
-
HTTP 状态码(200、400、404、500 等)
-
响应数据(JSON、字符串等)
-
HTTP 响应头
-
文件下载
-
重定向
ActionResult的继承关系
非泛型的 ActionResult 才具有类似下面的继承体系(部分类型):
IActionResult(接口)
│
└── ActionResult(抽象类)
│
├── ObjectResult
│ ├── OkObjectResult
│ ├── NotFoundObjectResult
│ └── BadRequestObjectResult
│
├── StatusCodeResult
│ ├── OkResult
│ ├── NotFoundResult
│ └── BadRequestResult
│
├── JsonResult
├── ContentResult
└── FileResult
ActionResult<T>的内部结构

为了方便理解,我们可以将它的源码简化为:
public sealed class ActionResult<T>
{
public T? Value { get; }
public ActionResult? Result { get; }
public ActionResult(T value)
{
Value = value;
}
public ActionResult(ActionResult result)
{
Result = result;
}
}
注意,这只是简化后的代码,并非完整源码。
它内部主要包含两个属性:
| 属性 | 类型 | 作用 |
|---|---|---|
| Value | T |
存储业务数据 |
| Result | ActionResult |
存储操作结果 |
因此,可以将 ActionResult<T> 理解为一个包装类(Wrapper Class)。
ActionResult<T>
│
├── Value : T
│
└── Result : ActionResult
通常由其中一个属性承载本次操作的返回结果。
举例说明:
public ActionResult<int> GetCJ3(int id)
{
if (id == 1)
{
return Ok("22");
}
else
{
return NotFound("未能找到");
}
}
一、 return Ok("22");相当于什么?
首先,Ok() 是 ControllerBase 类中定义的一个方法。
它的源码核心逻辑如下:
public virtual OkObjectResult Ok(object? value)
{
return new OkObjectResult(value);
}
因此:
return Ok("22");
相当于:
return new OkObjectResult("22");
但是,你的方法声明的返回类型是 ActionResult<int>,而不是 OkObjectResult。
所以编译器还会执行一次隐式类型转换。
最终相当于:
return new ActionResult<int>(
new OkObjectResult("22")
);
整个类型转换过程如下:
Ok("22")
↓
new OkObjectResult("22")
↓ 隐式类型转换
new ActionResult<int>(
new OkObjectResult("22")
)
最终 HTTP 响应:
StatusCode = 200
响应内容 = "22"
二、 return NotFound("未能找到");相当于什么?
同样,NotFound() 也是 ControllerBase 类中定义的方法。
其源码核心逻辑如下:
public virtual NotFoundObjectResult NotFound(object? value)
{
return new NotFoundObjectResult(value);
}
因此:
return NotFound("未能找到");
相当于:
return new NotFoundObjectResult("未能找到");
经过隐式类型转换后,最终相当于:
return new ActionResult<int>(
new NotFoundObjectResult("未能找到")
);
整个类型转换过程:
NotFound("未能找到")
↓
new NotFoundObjectResult("未能找到")
↓ 隐式类型转换
new ActionResult<int>(
new NotFoundObjectResult("未能找到")
)
最终 HTTP 响应:
StatusCode = 404
响应内容 = "未能找到"
三、将你的代码完全展开
下面三种写法在你的例子中是等价的。
写法一:使用 ControllerBase 提供的辅助方法
public ActionResult<int> GetCJ3(int id)
{
if (id == 1)
{
return Ok("22");
}
else
{
return NotFound("未能找到");
}
}
写法二:手动创建 ActionResult 的派生类对象
public ActionResult<int> GetCJ3(int id)
{
if (id == 1)
{
return new OkObjectResult("22");
}
else
{
return new NotFoundObjectResult("未能找到");
}
}
写法三:手动创建 ActionResult 泛型包装对象
public ActionResult<int> GetCJ3(int id)
{
if (id == 1)
{
return new ActionResult<int>(
new OkObjectResult("22")
);
}
else
{
return new ActionResult<int>(
new NotFoundObjectResult("未能找到")
);
}
}
第三种写法最能够体现其内部实现原理,而第一种写法是实际项目中最常用的。
四、为什么能够进行隐式类型转换?
因为存在以下继承关系:
IActionResult
└── ActionResult
└── ObjectResult
├── OkObjectResult
└── NotFoundObjectResult
而 ActionResult<T> 内部定义了两个隐式类型转换运算符:
public static implicit operator ActionResult<T>(T value)
{
return new ActionResult<T>(value);
}
public static implicit operator ActionResult<T>(
ActionResult result)
{
return new ActionResult<T>(result);
}
由于 OkObjectResult 和 NotFoundObjectResult 都是 ActionResult 的派生类,因此可以使用第二个隐式类型转换运算符。
五、为什么声明的是 int,却能够返回 string?
这是你这段代码中非常值得注意的一个知识点。
public ActionResult<int> GetCJ3(int id)
{
return Ok("22");
}
明明声明的泛型参数是 int,为什么可以返回字符串 "22"?
因为 ActionResult<int> 内部有两个属性:
public int Value { get; }
public ActionResult? Result { get; }
当你使用不同的返回语句时,实际上使用的是不同的属性。
| 返回语句 | Value 属性 | Result 属性 | 能否编译 |
|---|---|---|---|
return 22; |
22 | null | 可以 |
return "22"; |
--- | --- | 不可以 |
return Ok(22); |
0 | OkObjectResult | 可以 |
return Ok("22"); |
0 | OkObjectResult | 可以 |
return NotFound("未能找到"); |
0 | NotFoundObjectResult | 可以 |
这里的 0 是 int 类型的默认值。
例如:
return 22;
相当于:
return new ActionResult<int>(22);
内部状态:
ActionResult<int>
├── Value = 22
└── Result = null
而:
return Ok("22");
相当于:
return new ActionResult<int>(
new OkObjectResult("22")
);
内部状态:
ActionResult<int>
├── Value = 0
└── Result = OkObjectResult
├── StatusCode = 200
└── Value = "22"
注意,这里有两个不同层级的 Value 属性,不要混淆。
六、常用返回方法及其对应的对象
| Controller 方法 | 等价的对象创建方式 | HTTP 状态码 |
|---|---|---|
Ok() |
new OkResult() |
200 |
Ok(data) |
new OkObjectResult(data) |
200 |
NotFound() |
new NotFoundResult() |
404 |
NotFound(data) |
new NotFoundObjectResult(data) |
404 |
BadRequest() |
new BadRequestResult() |
400 |
BadRequest(data) |
new BadRequestObjectResult(data) |
400 |
NoContent() |
new NoContentResult() |
204 |
Unauthorized() |
new UnauthorizedResult() |
401 |
Unauthorized(data) |
new UnauthorizedObjectResult(data) |
401 |
StatusCode(500) |
new StatusCodeResult(500) |
500 |
七、实际开发中建议的写法
虽然你的代码可以正常编译,但是由于声明的业务数据类型是 int,成功时最好也返回 int 类型的数据,以避免实际响应类型与 API 声明类型不一致,进而影响 Swagger/OpenAPI 文档以及客户端的反序列化。
public ActionResult<int> GetCJ3(int id)
{
if (id == 1)
{
return Ok(22);
}
else
{
return NotFound("未能找到");
}
}
也可以简化为:
public ActionResult<int> GetCJ3(int id)
{
if (id == 1)
{
return 22;
}
return NotFound("未能找到");
}
最核心的知识点:
ActionResult<T> 的泛型参数 T 约束的是直接存放在其 Value 属性中的业务数据类型,而不是其 Result 属性内部所封装的数据类型。
所以 ActionResult<int> 并不能强制所有 HTTP 响应内容都是 int 类型。