获取 ArgumentException:升级到 .NET Core 2.2 后,使用 ActionResult<JsonResult>> 作为返回类型的类型参数无效

Ria*_*son 5 c# api json .net-core .net-core-2.2

当我尝试访问任何像下面所示的代码那样构建的 API 端点时,我收到以下运行时错误:

ArgumentException: Invalid type parameter 
'Microsoft.AspNetCore.Mvc.JsonResult' specified for 'ActionResult<T>'.
Run Code Online (Sandbox Code Playgroud)

错误

此代码在 .NET Core 2.1 中的应用程序中按预期运行。升级到 .NET Core 2.2 后,这个问题就被打破了。

这是控制器中我的一个端点的代码。

    [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);
        }
    }
Run Code Online (Sandbox Code Playgroud)

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

Dej*_*jan 5

JsonResult正如您在文档中看到的那样,已经实现IActionResult并继承了。因此,如果您还想返回其他类型的结果,则应该返回。ActionResultTask<IActionResult>

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