哪个 class 会继承项目 ASP.NET Core Web API 中的控制器?

Which class would inherit a Controller in a project ASP.NET Core Web API?

我正在按照教程构建 Web API 应用程序,但我不明白控制器必须从哪个 class 继承。

在我的教程中解释为继承自ControllerBase,但是在网络上我看到继承自ApiController的Controller。

最好的路径是什么?

"I am following a tutorial to build Web API Application, but I do not understand from which class a controller has to inherit?"

First of All you didn't shared which document you are following, on the other than it depends which kind of asp.net or asp.net core project controller you have taken. For example if you have taken asp.net core controller it should be inherited from ControllerBase if you have taken asp.net MVC it should be inherited from Controller but if you have taken only asp.net web API controller it should be inherited from ApiController as you may know Web API controllers inherited from ApiController

所以这里是你困惑的细节:

Asp.net Web API2 Controller Inherited From ApiController:

It has been developed on asp.net framework 4.8 before .Net core.

Asp.net Core MVC Controller Inherited From Controller:

Developed on asp.net core MVC

Asp.net Core Controller Inherited From ControllerBase:

All asp.net core no matter MVC or Web API Inherited from ControllerBase. After release version of asp.net core 2.1, you wouldn't get anything Inherited ApiController all from ControllerBase and curently on .Net 5.0 or .Net 6.0

"In my tutorial is explained to inherit from ControllerBase, but around the web I saw that Controller who inherit from ApiController?"

这取决于您正在处理的 asp.netasp.net core 项目的版本。如果您使用 asp.net core 5.0 或后者,则默认情况下应使用 ControllerBase 作为约定。

Note: If you would like to know more details on diffrent kind of ASP.NET MVC and Web API with controllers or even ASP.NET Core you can check here our official document

希望它能为您提供相应的指导。