Getting ArgumentException: Invalid type parameter using ActionResult<JsonResult>> as a return type after upgrading to .NET Core 2.2

Viewed 2877

I'm getting the following runtime error when trying to hit any of my API endpoints that are built like the code seen below:

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

error

This code functioned as expected in my app in .NET Core 2.1. After upgrading to .NET Core 2.2, this broke.

Here's the code for one of me endpoints in a 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);
        }
    }

Anyone have any insight on what changed between 2.1 and 2.2 that would cause this? I can't even find anyone else mentioning a similar issue or documentation on the change online.

1 Answers

JsonResult already implements IActionResult and inherits from ActionResult as you can see in the docs. So you should return Task<IActionResult> in case you want to return other types of results too.

However, I suggest you consider to not try and catch general exceptions as these will anyways be handled by the system and a status code of 500 will be returned.

Related