MVC Core IActionResult的含义 [英] MVC Core IActionResult meaning

查看:132
本文介绍了MVC Core IActionResult的含义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

What is an IActionResult? I tried looking at MSDN and other sites, but need general, common easy to understand answer.

MSDN IActionResult

示例:

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

推荐答案

通俗地说,IActionResult类型是操作结果的基本抽象.它用作代表特定响应类型的其他派生操作结果的基础,其中有很多.

In general terms IActionResult type is a base abstraction of an action result. It is used as the base of other derived action results that represent specific response types, of which there are many.

参考 Asp.Net核心行动结果介绍了

IActionResult和ActionResult

IActionResult和ActionResult充当其他操作结果的容器,因为IActionResult是一个接口,而ActionResult是其他操作结果继承的抽象类.因此,无法像其他操作结果一样对它们进行更新和返回.从可用性的角度来看,IActionResult和ActionResult没有太大区别,但是由于IActionResult是预期的操作结果合同,因此最好使用它而不是ActionResult.应该使用IActionResult/ActionResult来赋予我们更大的灵活性,例如当我们需要根据用户交互返回不同类型的响应时.

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.

引用官方文档,请在此处找到

To quote official documentation Found here Controller action return types in ASP.NET Core Web API

当动作中可能有多个ActionResult返回类型时,IActionResult返回类型是适当的. ActionResult类型表示各种HTTP状态代码.属于此类别的一些常见返回类型为BadRequestResult(400),NotFoundResult(404)和OkObjectResult(200).

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

这篇关于MVC Core IActionResult的含义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆