英文:
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<IEnumerable<ZapResult>>
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<ZapResult>
.
Indeed, both Ok()
and BadRequest()
return objects derived from ActionResult
(not ActionResult<T>
), but in either case everything works fine and JSON is returned to the client:
[HttpGet, FormatFilter]
public ActionResult<IEnumerable<ZapResult>> OnGet([FromQuery] ZapScan scan)
{
return HandleRequest(scan);
}
private ActionResult<IEnumerable<ZapResult>> HandleRequest(ZapScan scan)
{
if (ValidateScan(scan)) {
IEnumerable<ZapResult> zapResults = _zapDispatcher.Dispatch(scan);
return Ok(zapResults);
}
else
{
var clientIp = HttpContext.Request.HttpContext.Connection.RemoteIpAddress;
var requestUrl = UriHelper.GetDisplayUrl(HttpContext.Request);
_logger.LogError("{0}: Invalid request: [{1}] {2} ({3})", MethodBase.GetCurrentMethod().Name, clientIp, requestUrl, scan);
return BadRequest(scan);
}
}
To me it seems like anything can be converted to ActionResult<T>
, and that such return type is completely generic?
英文:
I wonder why an ASP.NET Core action returning an ActionResult<IEnumerable<ZapResult>>
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<ZapResult>
.
Indeed, both Ok()
and BadRequest()
return objects derived from ActionResult
(not ActionResult<T>
), but in either case everything works fine and JSON is returned to the client:
[HttpGet, FormatFilter]
public ActionResult<IEnumerable<ZapResult>> OnGet([FromQuery] ZapScan scan)
{
return HandleRequest(scan);
}
private ActionResult<IEnumerable<ZapResult>> HandleRequest(ZapScan scan)
{
if (ValidateScan(scan)) {
IEnumerable<ZapResult> zapResults = _zapDispatcher.Dispatch(scan);
return Ok(zapResults);
}
else
{
var clientIp = HttpContext.Request.HttpContext.Connection.RemoteIpAddress;
var requestUrl = UriHelper.GetDisplayUrl(HttpContext.Request);
_logger.LogError("{0}: Invalid request: [{1}] {2} ({3})", MethodBase.GetCurrentMethod().Name, clientIp, requestUrl, scan);
return BadRequest(scan);
}
}
To me it seems like anything can be converted to ActionResult<T>
, and that such return type is completely generic?
答案1
得分: 1
BadRequestResult
(由BadRequest()
返回)派生自ActionResult
。
ActionResult<T>
可以通过它实现的IConvertToActionResult
接口隐式转换为ActionResult
。
因此,返回类型是兼容的,这就是为什么它们可以互换使用。
BadRequestResult
有一个构造函数,它接受任何object
并将其序列化为字符串,然后原样输出给客户端。
英文:
BadRequestResult
(returned by BadRequest()
) derives from ActionResult
.
ActionResult<T>
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论