对于 webapi vs 代码项目,“IServiceCollection”不包含使用 dot net 5 的“AddApiVersioning”定义

lcj*_*lcj 1 .net .net-core asp.net-core-webapi aspnet-api-versioning

我正在尝试将版本控制 (Microsoft.AspNet.WebApi.Versioning) 与 dotnet 5 一起用于 webapi 项目。我已经添加了 nuget 包,但出现以下错误:

“IServiceCollection”不包含“AddApiVersioning”的定义,并且找不到接受“IServiceCollection”类型的第一个参数的可访问扩展方法“AddApiVersioning”(您是否缺少 using 指令或程序集引用?)

在这一行:

services.AddApiVersioning();
Run Code Online (Sandbox Code Playgroud)

在我的 Startup.cs 中

我的 .csproj 文件如下所示:

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

我尝试遵循一些示例,例如这个这个这个,我应该有以下“使用指令”,我有:

using Microsoft.AspNetCore.Mvc;
Run Code Online (Sandbox Code Playgroud)

有人面临同样的挑战吗?.net 5 是否支持版本控制?

pok*_*oke 5

您正在使用包Microsoft.AspNet.WebApi.Versioning,它是 ASP.NET Web API 的 API 版本控制包,ASP.NET Web API 是 .NET Framework 的经典 Web API 框架。

\n

您使用的是 ASP.NET Core,因此您需要Microsoft.NET Core 包。AspNetCore.Mvc.版本控制。该软件包也是您链接的教程中提到的\xe2\x80\x99s 软件包。

\n

然而,\xe2\x80\x99s 很容易混淆这些包,因为它们都来自GitHub 上的同一存储库,该存储库还为特殊用例生成了一些附加包。

\n