编程语言
首页 > 编程语言> > c#-MVC Core IActionResult的含义

c#-MVC Core IActionResult的含义

作者:互联网

什么是IActionResult?我尝试查看MSDN和其他站点,但需要通用,通用的易于理解的答案.

MSDN IActionResult

例:

public IActionResult About() {
    ViewData["Message"] = "About Page";
    return View();
}

解决方法:

一般而言,IActionResult类型是操作结果的基本抽象.它用作表示特定响应类型的其他派生操作结果的基础,其中有很多响应类型.

参考Asp.Net Core Action Results Explained

IActionResult and ActionResult

IActionResult and ActionResult work as a container for other action results, in that IActionResult is an interface and ActionResult is an abstract class that other action results inherit from. So they can’t be newed up and returned like other action results. IActionResult and ActionResult have not much of a different from usability perspective, but since IActionResult is the intended contract for action results, it’s better to use it as opposed to ActionResult. IActionResult/ActionResult should be used to give us more flexibility, like when we need to return different type of response based on user interaction.

引用官方文档在这里找到Controller action return types in ASP.NET Core Web API

The IActionResult return type is appropriate when multiple ActionResult return types are possible in an action. The ActionResult types represent various HTTP status codes. Some common return types falling into this category are BadRequestResult (400), NotFoundResult (404), and OkObjectResult (200).

标签:asp-net-core,asp-net-core-mvc,c
来源: https://codeday.me/bug/20191109/2012068.html