ASP.NET何时使用IActionResult,何时使用IResult?

huangapple go评论65阅读模式
英文:

ASP.NET when to use IActionResult and when to use IResult?

问题

我试图理解IActionResult和IResult作为返回类型的优缺点以及何时使用适当的选项。根据我收集的信息,IActionResult与IResult类似,但在如何处理结果方面具有更多选项。

英文:

I am trying to understand what the pros and cons are of IActionResult and IResult as return types and when to use the approopriate one. From what i've gathered IActionResult is just like IResult but with more options on how to handle the result?

答案1

得分: 2

IActionResult :: 定义一个表示操作方法结果的契约。ASP.NET Core 7

IActionResult 允许您执行一些基于操作的附加操作,如重定向、更改响应的格式等。

在您的 Web 应用程序(MVC)的一侧使用 IActionResult,因为它提供了更多处理请求的方法。

IResult :: 定义一个表示 HTTP 端点结果的契约。ASP.NET Core 7

ASP.NET Core 是一个新的静态 Results 实用类,用于生成常见的 HTTP 响应作为 IResults。IResult 是在 Minimal APIs 中引入的新返回类型。

英文:

IActionResult :: Defines a contract that represents the result of an action method.ASP.NET Core 7

IActionResult allows you to provide some more operations based on your actions like redirecting, changing the response's format etc.

Use IActionResult on the side of your web application - MVC, since it gives you more approaches to handle requests.

IResult :: Defines a contract that represents the result of an HTTP endpoint. ASP.NET Core 7

ASP.NET Core is a new static Results utility class to produce common HTTP responses as IResults. IResult is a new return type that got introduced with Minimal APIs.

答案2

得分: 1

IActionResult:定义了表示操作方法结果的合同。

示例:

public IActionResult OkResult()
{
    return Ok();
}

或者

public IActionResult NoContentResult()
{
    return NoContent();
}

IResult:定义了表示HTTP终结点结果的合同。

示例:

class CustomHTMLResult : IResult
{
    private readonly string _htmlContent;
    public CustomHTMLResult(string htmlContent)
    {
        _htmlContent = htmlContent;
    }
    public async Task ExecuteAsync(HttpContext httpContext)
    {
        httpContext.Response.ContentType = MediaTypeNames.Text.Html;
        httpContext.Response.ContentLength = Encoding.UTF8.GetByteCount(_htmlContent);
        await httpContext.Response.WriteAsync(_htmlContent);
    }
}
  • (第1行) 'CustomHTMLResult' 实现了 'Microsoft.AspNetCore.Http.IResult'。
  • (第3-7行) 注入 HTML 结果。
  • (第8行) 'ExecuteAsync' 方法在初始化 'CustomHTMLResult' 对象时自动执行。
  • (第10-12行) 更新 'HttpContext' 对象,定义我们的HTML响应,如定义 'ContentType'、'ContentLength'。
static class CustomResultExtensions
{
    public static IResult HtmlResponse(this IResultExtensions extensions, string html)
    {
        return new CustomHTMLResult(html);
    }
}
  • 'CustomResultExtensions' 是一个静态类,我们可以在其中定义自定义响应的扩展方法。
英文:

IActionResult: Defines a contract that represents the result of an action method.

Example:

public IActionResult OkResult()
{
    return Ok();
}

or

public IActionResult NoContentResult()
{
    return NoContent();
}

IResult: Defines a contract that represents the result of an HTTP endpoint.

Example:

class CusomtHTMLResult : IResult
{
    private readonly string _htmlContent;
    public CusomtHTMLResult(string htmlContent)
    {
        _htmlContent = htmlContent;
    }
    public async Task ExecuteAsync(HttpContext httpContext)
    {
        httpContext.Response.ContentType = MediaTypeNames.Text.Html;
        httpContext.Response.ContentLength = Encoding.UTF8.GetByteCount(_htmlContent);
        await httpContext.Response.WriteAsync(_htmlContent);
    }
}
  • (Line: 1) The 'CustomHTMLResult' implementing the
    'Microsoft.AspNetCore.Http.IResult'.

  • (Line: 3-7) Injecting the HTML result.

  • (Line: 8) The 'ExecuteAsync' method gets automatically executed on
    initializing 'CustomHTMLResult' object.

  • (Line: 10-12) Updating the 'HttpContext' object with our HTML
    response, like defining 'ContentType', 'ContentLength'.

     static class CustomResultExtensions
     {
     public static IResult HtmlResponse(this IResultExtensions extensions, string html)
     {
         return new CusomtHTMLResult(html);
     }
    

    }

  • The 'CustomResultExtions' is a static class. where we can define the extension method of our custom response.

huangapple
  • 本文由 发表于 2023年2月6日 17:26:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/75359441.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定