gpt4 book ai didi

c# - MVC Core IActionResult 含义

转载 作者:太空狗 更新时间:2023-10-30 00:48:08 25 4
gpt4 key购买 nike

什么是 IActionResult?我尝试查看 MSDN 和其他站点,但需要一般的、常见的、易于理解的答案。

MSDN IActionResult

示例:

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

最佳答案

一般来说,IActionResult 类型是 Action 结果的基础抽象。它被用作代表特定响应类型的其他派生操作结果的基础,其中有很多。

引用 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).

关于c# - MVC Core IActionResult 含义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49503802/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com