gpt4 book ai didi

c# - 在 .NET Core WebApi 项目中找不到 ApiController

转载 作者:太空狗 更新时间:2023-10-29 21:52:47 24 4
gpt4 key购买 nike

我在 Visual Studio Code 中创建了一个新的 .NET Core WebAPI 项目。所有依赖项都已恢复。该项目使用以下 SDK 和运行时:

  • .NET 核心 SDK 2.1.302
  • Microsoft.AspNetCore.All 2.1.2
  • Microsoft.AspNetCore.App 2.1.2
  • Microsoft.NETCore.App 2.1.2

我的 Controller 类(包括 Intellisense View 的屏幕截图):

using Microsoft.AspNetCore.Mvc;
using System.Collections.Generic;
using System.Linq;

namespace Gmn.Controllers
{
[Route("api/[controller]")]
[ApiController]
public class MyController : ControllerBase
{
public MyController() {

}
}
}

Syntax highlighting shows ApiController not found

构建错误:

error CS0246: The type or namespace name 'ApiControllerAttribute' could not be found (are you missing a using directive or an assembly reference?)

同样在Microsoft.AspNetCore.Mvc程序集中的Route属性解析正确,没有报错。所以,我知道程序集存在。

根据 MS 文档,属性在此程序集中定义:

MS Docs for ApiControllerAttribute

经过 3 小时的搜索,我无法弄清楚这一点。我是 .NET Core 的新手,但不是 .NET。

有什么解决这个问题的建议吗?谢谢。

最佳答案

我终于明白了。 .csproj 文件未指向 TargetFrameworkPackageReference 的正确版本。他们指向 2.0 版本。 2.0 甚至没有安装,但没有关于它的错误。 (来自 Visual Studio,我已经习惯了为我完成的所有魔法!)

<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>netcoreapp2.1</TargetFramework>
</PropertyGroup>

...

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.All" Version="2.1.2" />
</ItemGroup>

...

</Project>

关于c# - 在 .NET Core WebApi 项目中找不到 ApiController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51521508/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com