I am following a tutorial to build Web API Application, but I do not understand from which class a controller has to inherit.
In my tutorial is explained to inherit from ControllerBase, but around the web I saw that Controller who inherit from ApiController.
What is the best path to follow?
CodePudding user response:
"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
orasp.net core
projectcontroller
you have taken. For example if you have takenasp.net core controller
it should be inherited fromControllerBase
if you have takenasp.net MVC
it should be inherited fromController
but if you have taken onlyasp.net web API controller
it should be inherited fromApiController
as you may know
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 matterMVC
orWeb API
Inherited from ControllerBase. After release version of asp.net core 2.1, you wouldn't get anything InheritedApiController
all fromControllerBase
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?"
It depends on which version of
asp.net
orasp.net core
project you are working on. If you are usingasp.net core 5.0
or latter than it should useControllerBase
by default as convension.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
Hope it would guided you accordingly.