Search code examples
.net.net-coreasp.net-core-webapiaspnet-api-versioning

'IServiceCollection' does not contain a definition for 'AddApiVersioning' using dot net 5 for a webapi vs code project


I am trying to use versioning (Microsoft.AspNet.WebApi.Versioning) with dotnet 5 for a webapi project. I have added the nuget package but get the following error:

'IServiceCollection' does not contain a definition for 'AddApiVersioning' and no accessible extension method 'AddApiVersioning' accepting a first argument of type 'IServiceCollection' could be found (are you missing a using directive or an assembly reference?)

on this line:

services.AddApiVersioning();

in my Startup.cs

My .csproj file looks like this:

<Project Sdk="Microsoft.NET.Sdk.Web">
  <PropertyGroup>
    <TargetFramework>net5.0</TargetFramework>
  </PropertyGroup>
  <ItemGroup>
    <PackageReference Include="Microsoft.Identity.Web" Version="1.0.0"/>
    <PackageReference Include="Microsoft.Identity.Web.UI" Version="1.0.0"/>
    <PackageReference Include="Swashbuckle.AspNetCore" Version="5.6.3"/>
    <PackageReference Include="Microsoft.Data.SqlClient" Version="2.0.1"/>
    <PackageReference Include="Dapper" Version="2.0.35"/>
    <PackageReference Include="Azure.Storage.Blobs" Version="12.4.1"/>
    <PackageReference Include="Microsoft.AspNet.WebApi.Versioning" Version="4.1.1"/>
  </ItemGroup>
</Project>

I tried to follow some examples, like this one and this one and this one, I should have the following 'using directive', which I have:

using Microsoft.AspNetCore.Mvc;

Is anybody having the same challenge? Is Versioning supported yet in .net 5?


Solution

  • You are using the package Microsoft.AspNet.WebApi.Versioning which is the API versioning package for ASP.NET Web API, the classic web API framework for .NET Framework.

    You are using ASP.NET Core, so you will need the package Microsoft.AspNetCore.Mvc.Versioning instead. This package is also the one that’s mentioned in the tutorials you linked.

    It’s very easy to confuse these packages however since they both come from the same repository on GitHub which also produces a few additional packages for specialized use cases.