gpt4 book ai didi

c# - 获取 ArgumentException : Invalid type parameter using ActionResult> as a return type after upgrading to . NET Core 2.2

转载 作者:太空宇宙 更新时间:2023-11-03 12:02:05 27 4
gpt4 key购买 nike

当我尝试访问我的任何 API 端点时,我收到了以下运行时错误,这些端点的构建方式如下所示:

ArgumentException: Invalid type parameter 
'Microsoft.AspNetCore.Mvc.JsonResult' specified for 'ActionResult<T>'.

error

此代码在我的 .NET Core 2.1 应用中按预期运行。升级到 .NET Core 2.2 后,问题就解决了。

这是 Controller 中其中一个端点的代码。

    [HttpGet]
public async Task<ActionResult<JsonResult>> Get()
{
try
{
MongoConnector mongo = new
MongoConnector(_configService.GetMongoConnectionString());

List<BsonDocument> queues = await mongo.AggregateQueues(
(aggregator) => aggregator
.Match(CommonAggregation.SinceYesterday)
.Sort(CommonAggregation.SortByDate)
.Group(QueueAggregators.GroupQueuesByMessagingName)
);


List<QueueHealth> queueHealth = await MongoConnector.DeserializeList<QueueHealth>(queues);
JsonResult result = new JsonResult(queueHealth);
result.Value = queueHealth;
result.ContentType = "application/json";
result.StatusCode = 200;

return result;

}
catch (Exception ex)
{
logger.Error(ex, "An exception was thrown within /api/MessageQueue");

return new StatusCodeResult(InternalServerError);
}
}

有人知道 2.1 和 2.2 之间发生了什么变化会导致这种情况吗?我什至找不到其他人提到类似的问题或在线更改的文档。

最佳答案

JsonResult已经实现 IActionResult并继承自 ActionResult正如您在 docs 中看到的那样.所以你应该返回 Task<IActionResult>如果您还想返回其他类型的结果。

但是,我建议您考虑不要 try catch 一般异常,因为系统无论如何都会处理这些异常,并且会返回状态代码 500。

关于c# - 获取 ArgumentException : Invalid type parameter using ActionResult<JsonResult>> as a return type after upgrading to . NET Core 2.2,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56656450/

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