You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

新建.NET WebAPI项目后出现命名空间缺失等编译错误如何解决

新建Web API项目命名空间缺失报错解决方案

项目创建操作

通过以下命令新建Web API项目:

dotnet new webapi -o TodoApi
cd TodoApi
dotnet add package Microsoft.EntityFrameworkCore.InMemory --prerelease
code -r ../TodoApi

问题表现

项目创建完成后出现全量命名空间缺失问题,共触发26个编译错误,典型报错示例如下:

  • The type or namespace name 'ILogger<>' could not be found (are you missing a using directive or an assembly reference?) [PaymentApi](未找到类型或命名空间名称'ILogger<>',是否缺少using指令或程序集引用)
  • The name 'WebApplication' does not exist in the current context [TechnePaymentApi](当前上下文中不存在名称'WebApplication')
  • A namespace cannot directly contain members such as fields or methods [TechnePaymentApi](命名空间不能直接包含字段、方法等成员)

错误截图:
错误截图

已验证解决方案

该问题由VS Code和C#插件版本过低导致,将两者升级到最新版本后重新创建ASP.NET Core 6.0项目即可消除所有报错。

内容的提问来源于stack exchange,提问作者hanushic

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.26 18:45:04