C# asp.net web api 自动创建具有顶级语句类型的项目,有什么办法可以避免这种情况吗?

C# asp.net web api automatically creates projects with top-level statements type is there any way to avoid that?

大家好,当我创建一个新的 asp.net 核心 web api 项目时,我认为 c# 创建的项目具有顶级 statements.Here 是新创建的 web api项目的 program.cs 文件

我是网络 api 项目的新手,所以我不确定我是否无意中做了什么,但这就是我得到的

根据文档,从 .NET 6 开始,模板使用顶级语句。他们说您应该创建一个 .NET 5 项目,然后手动将其更改为 .NET 6 作为解决方法,如果您不想使用顶级语句。


参考文献:

The C# templates for .NET 6 use top level statements.

Tutorial: Explore ideas using top-level statements to build code as you learn

While a .NET 6 console app template generates the new style of top-level statements programs, using .NET 5 doesn't. By creating a .NET 5 project, you'll receive the old program style. Then, you can edit the project file to target .NET 6 but retain the old program style for the Program.cs file.

Use the old program style

这是从 c# 9.0 开始的功能。您的项目没有配置错误。