OData .Net Core 3.1 with Swagger

Object reference not set to an instance of an object. at Microsoft.AspNetCore.Mvc.Routing.ApiVersionMatcherPolicy

I recently upgraded .net core 2.2 web api with Odata and Swagger to 3.1 following Microsoft’s recent endpoint routing support with OData 7.40. All was going great until I realised certain API endpoints were failing with “Object reference not set to an instance of an object. at Microsoft.AspNetCore.Mvc.Routing.ApiVersionMatcherPolicy”. On closer inspection the endpoints failing were all child resources. For example ../api/company/1 works fine ../api/company/1/staff and ../api/company/1/staff/1 were failing with

Solution

After a bit of digging (I removed the OData versioning Nuget packages as these are now served via .Net) I realised I had the [ApiController] attribute decorating every Controller. Looking at some of the recent examples online I noticed this wasn’t present. Once I’d removed the attribute I was back in business.

Leave a Reply