简体   繁体   中英

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

hello guys when I create a new asp.net core web api project, I think c# creates projects with top-level statements.Here is the freshly created web api project's program.cs file 在此处输入图像描述

I am new to web api projects so I am not sure if I did something unintentionally but this is what I got

According to the docs, starting with .NET 6, the templates use top level statements. They say you should create a .NET 5 project and then manually change it to .NET 6 as a workaround if you don't want to use top level statements.


References:

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

This is feature starting with c# 9.0. There is no misconfiguration in your project.

Use:

dotnet new webapi --use-program-main -o <project>

Creating a new ASP.Net-Project via the GUI of Visual Studio, the marked Option 'Do not use top-level statements' should do what yo want. Its in the last Step of creating a new Project:

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM