1

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

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

在这条线上:

services.AddApiVersioning();

在我的 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>

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

using Microsoft.AspNetCore.Mvc;

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

4

2 回答 2

3

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

您正在使用 ASP.NET Core,因此您将需要Microsoft 包。AspNetCore.Mvc .Versioning代替。该软件包也是您链接的教程中提到的软件包。

但是很容易混淆这些包,因为它们都来自GitHub 上的同一个存储库,该存储库还为专门的用例生成了一些额外的包。

于 2021-04-05T14:32:50.943 回答
0

对于.net6,我从nuget手动添加

Microsoft.AspNetCore.Mvc.Versioning

并添加到 Program.cs

using Microsoft.AspNetCore.Mvc;

它运作良好。带有 .net6 组合的 Visual Studio 2022 存在这样的问题。我曾多次遇到过这个问题。

于 2022-01-02T17:26:01.130 回答