Asp.net Core之前所有的Action返回值都是<font style="color:rgb(18, 18, 18);">ActionResult</font><font style="color:rgb(18, 18, 18);">Json()</font>, <font style="color:rgb(18, 18, 18);">File()</font>等方法返回的都是ActionResult的子类。并且Core把MVC跟WebApi合并之后Action的返回值体系也有了很大的变化。

一、ActionResult类

ActionResult类是最常用的返回值类型。基本沿用了之前Asp.net MVC的那套东西,使用它大部分情况都没问题。比如用它来返回视图,返回json,返回文件等等。如果是异步则使用Task。
  1. public class TestController:Controller
  2. {
  3. public ActionResult Index()
  4. {
  5. return View();
  6. }
  7. public ActionResult MyFile()
  8. {
  9. return File(newbyte[]{},"image/jpg");
  10. }
  11. public ActionResult MyJson()
  12. {
  13. return Json(new{ name ="json"});
  14. }
  15. public ActionResult Ok()
  16. {
  17. return Ok();
  18. }
  19. }

二、IActionResult接口

ActionResult类实现了IActionResult接口所以能用ActionResult的地方都可以使用IActionResult来替换。同样异步的话使用Task包起来做为返回值。
  1. public class ITestController : Controller
  2. {
  3. public IActionResult Index()
  4. {
  5. return View();
  6. }
  7. public IActionResult MyFile()
  8. {
  9. return File(newbyte[]{},"image/jpg");
  10. }
  11. public IActionResult MyJson()
  12. {
  13. return Json(new{ name ="json"});
  14. }
  15. public IActionResult HttpOk()
  16. {
  17. return Ok();
  18. }
  19. public async Task<IActionResult> AsyncCall()
  20. {
  21. await Task.Delay(1000);
  22. return Content("ok");
  23. }
  24. }

三、直接返回POCO类

Asp.net Core的Controller的Action可以把POCO类型(其实不一定是POCO类,可以是任意类型,但是使用的时候一般都返回viwemodel等POCO类)当做返回值,不一定非要是ActionResult或者IActionResult。Asp.net Core框架会帮我们自动序列化返回给前端,默认使用json序列化。同样异步的话使用Task包起来做为返回值。

  1. public class Person
  2. {
  3. public string Name{get;set;}
  4. public string Sex{get;set;}
  5. }
  6. public class ITestController:Controller
  7. {
  8. public Person GetPerson()
  9. {
  10. return new Person{Name="abc",Sex="f"};
  11. }
  12. public async Task<List<Person>> GetPersons()
  13. {
  14. await Task.Delay(1000);
  15. return new List<Person>{
  16. new Person{Name="abc",Sex="f"},
  17. new Person{Name="efg",Sex="m"}
  18. };
  19. }
  20. }

四、ActionResult< T >泛型类

当我们设计restful webapi系统的时候习惯使用POCO做为返回值。比如我们设计一个获取Person的api。通过 /person/001 url获取001号person。
  1. [Route("[controller]")]
  2. public class PersonController:Controller
  3. {
  4. IPersonRepository _repository;
  5. PersonController(IPersonRepository repository)
  6. {
  7. _repository = repository;
  8. }
  9. [HttpGet("{id}")]
  10. public PersonGet(string id)
  11. {
  12. return _repository.Get(id);
  13. }
  14. }
这个方法看起来好像没什么问题,但其实有个小问题。如果repository.Get方法没有根据id查找到数据,那么将会返回null。如果null做为Action的返回值,最后框架会转换为204的http status code。

.NET Core | Action的返回值类型 - 图1


204表示No Content 。做为restful api,204的语义在这里会有问题,这里比较适合的status code是404 NOT FOUND 。那么我们来改一下:
  1. [HttpGet("{id}")]
  2. public PersonGet(string id)
  3. {
  4. var person = _repository.Get(id);
  5. if(person ==null)
  6. {
  7. Response.StatusCode=404;
  8. }
  9. return person;
  10. }
现在如果查找不到person数据,则系统会返回404 Not Found 。

.NET Core | Action的返回值类型 - 图2


但是这看起来显然不够优雅,因为ControllerBase内置了NotFoundResult NotFound() 方法。这使用这个方法代码看起来更加清晰明了。继续改:
  1. [HttpGet("{id}")]
  2. public PersonGet(string id)
  3. {
  4. var person = _repository.Get(id);
  5. if(person ==null)
  6. {
  7. return NotFound();
  8. }
  9. return person;
  10. }
很不幸,这段代码VS会提示错误。因为返回值类型不一致。方法签名的返回值是Person,但是方法内部一会返回NotFoundResult,一会返回Person。

.NET Core | Action的返回值类型 - 图3
解决这个问题就该ActionResult< T >出场了。我们继续改一下:

  1. [HttpGet("{id}")]
  2. public ActionResult<Person> Get(string id)
  3. {
  4. var person = _repository.Get(id);
  5. if(person ==null)
  6. {
  7. return NotFound();
  8. }
  9. return person;
  10. }
现在VS已经不会报错了,运行一下也可以正常工作。但仔细想想也很奇怪,为什么返回值类型改成了ActionResult< Person >就不报错了呢?明明返回值类型跟方法签名还是不一致啊?

五、深入ActionResult< T >

接上面的问题,让我们看一下ActionResult的内部:

.NET Core | Action的返回值类型 - 图4
看到这里就明白了原来ActionResult< T >里面内置了2个implicit operator方法。implicit operator用于声明隐式类型转换。

  1. <font style="color:rgb(18, 18, 18);background-color:rgb(246, 246, 246);">publicstaticimplicitoperatorActionResult<TValue>(ActionResult result);</font>
表示ActionResult类型可以转换为ActionResult< TValue >类型。
  1. <font style="color:rgb(18, 18, 18);background-color:rgb(246, 246, 246);">publicstaticimplicitoperatorActionResult<TValue>(TValue value)</font>
表示TValue类型可以转换为ActionResult< TValue >类型。 因为有了这2个方法,当ActionResult或者TValue类型往ActionResult< T >赋值的时候会进行一次自动的类型转换。所以VS这里不会报错。

六、总结

  1. 大部分时候Action的返回值可以使用ActionResult/IActionResult
  2. 设计restful api的时候可以直接使用POCO类作为返回值
  3. 如果要设计既支持POCO类返回值或者ActionResult类为返回值的action可以使用ActionResult< T >作为返回值
  4. ActionResult< T >之所以能够支持两种类型的返回值类型,是因为使用了implicit operator内置了2个隐式转换的方法