Why can an ASP.NET Core action returning an ` ActionResult<IEnumerable<ZapResult>>` return a `BadRequest()` of any object?

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

Why can an ASP.NET Core action returning an ` ActionResult<IEnumerable<ZapResult>>` return a `BadRequest()` of any object?

问题

I wonder why an ASP.NET Core action returning an ActionResult&lt;IEnumerable&lt;ZapResult&gt;&gt; may return a BadRequest(scan), where scan is an object of a custom user-defined class?

In this case, I don't see any sensible way of converting ZapScan to IEnumerable&lt;ZapResult&gt;.

Indeed, both Ok() and BadRequest() return objects derived from ActionResult (not ActionResult&lt;T&gt;), but in either case everything works fine and JSON is returned to the client:

[HttpGet, FormatFilter]
public ActionResult&lt;IEnumerable&lt;ZapResult&gt;&gt; OnGet([FromQuery] ZapScan scan)
{
    return HandleRequest(scan);
}

private ActionResult&lt;IEnumerable&lt;ZapResult&gt;&gt; HandleRequest(ZapScan scan)
{
    if (ValidateScan(scan)) {
        IEnumerable&lt;ZapResult&gt; zapResults = _zapDispatcher.Dispatch(scan);
        return Ok(zapResults);
    }
    else
    {
        var clientIp = HttpContext.Request.HttpContext.Connection.RemoteIpAddress;
        var requestUrl = UriHelper.GetDisplayUrl(HttpContext.Request);
        _logger.LogError(&quot;{0}: Invalid request: [{1}] {2} ({3})&quot;, MethodBase.GetCurrentMethod().Name, clientIp, requestUrl, scan);
        return BadRequest(scan);
    }
}

To me it seems like anything can be converted to ActionResult&lt;T&gt;, and that such return type is completely generic?

英文:

I wonder why an ASP.NET Core action returning an ActionResult&lt;IEnumerable&lt;ZapResult&gt;&gt; may return a BadRequest(scan), where scan is an object of a custom user-defined class?

In this case, I don't see any sensible way of converting ZapScan to IEnumerable&lt;ZapResult&gt;.

Indeed, both Ok() and BadRequest() return objects derived from ActionResult (not ActionResult&lt;T&gt;), but in either case everything works fine and JSON is returned to the client:

[HttpGet, FormatFilter]
public ActionResult&lt;IEnumerable&lt;ZapResult&gt;&gt; OnGet([FromQuery] ZapScan scan)
{
    return HandleRequest(scan);
}

private ActionResult&lt;IEnumerable&lt;ZapResult&gt;&gt; HandleRequest(ZapScan scan)
{
    if (ValidateScan(scan)) {
        IEnumerable&lt;ZapResult&gt; zapResults = _zapDispatcher.Dispatch(scan);
        return Ok(zapResults);
    }
    else
    {
        var clientIp = HttpContext.Request.HttpContext.Connection.RemoteIpAddress;
        var requestUrl = UriHelper.GetDisplayUrl(HttpContext.Request);
        _logger.LogError(&quot;{0}: Invalid request: [{1}] {2} ({3})&quot;, MethodBase.GetCurrentMethod().Name, clientIp, requestUrl, scan);
        return BadRequest(scan);
    }
} 

To me it seems like anything can be converted to ActionResult&lt;T&gt;, and that such return type is completely generic?

答案1

得分: 1

BadRequestResult(由BadRequest()返回)派生自ActionResult
ActionResult&lt;T&gt;可以通过它实现的IConvertToActionResult接口隐式转换为ActionResult

因此,返回类型是兼容的,这就是为什么它们可以互换使用。

BadRequestResult有一个构造函数,它接受任何object并将其序列化为字符串,然后原样输出给客户端。

英文:

BadRequestResult (returned by BadRequest()) derives from ActionResult.
ActionResult&lt;T&gt; is implicitly convertible to ActionResult (via the IConvertToActionResult interface it implements).

Therefore the return types are compatible which is why they can be used interchangeably.

BadRequestResult has a constructor that takes any object and serializes it to a string that is output as-is to the client.

huangapple
  • 本文由 发表于 2020年1月3日 18:50:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/59577229.html
匿名

发表评论

匿名网友

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

确定