Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why derive from ControllerBase vs Controller for ASP.NET Core Web API?

I am following this tutorial for creating an ASP.NET Core Web API, and in the section on adding a controller, the tutorial gives code to replace the template code for a controller. One of the things that really caught my eye was that in the template code, I get:

TodoController : Controller 

Then in the tutorial code I'm supposed to use instead of that, I find:

[Route("api/[controller]")] [ApiController] TodoController : ControllerBase 

I'm very interested to know why it is necessary to derive from ControllerBase instead of Controller for a Web API controller. Why is this done?

like image 336
ProfK Avatar asked Mar 19 '19 10:03

ProfK


People also ask

Which class must Web API controllers derive from?

Web API controller is a class which can be created under the Controllers folder or any other folder under your project's root folder. The name of a controller class must end with "Controller" and it must be derived from System. Web.

What is the purpose of API controller attribute?

The ApiController attribute is commonly coupled with the ControllerBase class to enable REST-specific behavior for controllers, and it allows us to build HTTP APIs. First of all, it provides implicit model state validation, which means that we do not need to explicitly check the ModelState.

What is difference between MVC controller and Web API?

The Web API returns the data in various formats, such as JSON, XML and other format based on the accept header of the request. But the MVC returns the data in the JSON format by using JSONResult. The Web API supports content negotiation, self hosting. All these are not supported by the MVC.

What is ControllerBase class in .NET core?

The ControllerBase Class in ASP.NET Core provides many methods and properties to handle HTTP Requests and Responses. For example, if you want 200 Status codes from your action method, this provides one method.

What is the difference between controllerbase and controller and API?

When you write an API then ControllerBase matches your requirements better but both will work. Don't create a web API controller by deriving from the Controller class. Controller derives from ControllerBase and adds support for views, so it's for handling web pages, not web API requests.

What is API controller in ASP NET Core?

API Controllers – Creating API in ASP.NET Core. API controllers are just like regular controllers except that their action methods produce responses which contain data objects and are sent to the client without HTML markup. Since not all clients are browsers so to provide them with data we your API Controllers.

What is the difference between MVC and API controllers?

As of Core 2.0, your MVC and API controllers both derive from the Controller class, which derives from ControllerBase: As of Core 2.1 (and 2.2), the template-generated classes look a little different, where a Web controller is a child of the Controller class and an API controller is a child of ControllerBase.

Is it possible to create a web API from a controller?

Don't create a web API controller by deriving from the Controller class. Controller derives from ControllerBase and adds support for views, so it's for handling web pages, not web API requests. There's an exception to this rule: if you plan to use the same controller for both views and web APIs, derive it from Controller.


1 Answers

why it is necessary to derive from ControllerBase instead of Controller for a Web API controller.

It is not strictly necessary, just more to the point. The Controller class derives from ControllerBase and adds some members that are only needed to support Views.

Basically:

public abstract class Controller : ControllerBase {     public dynamic ViewBag { get; }     public virtual ViewResult View(object model) { }     // more View support stuff } 

When you write an API then ControllerBase matches your requirements better but both will work.

Per the documentation (emphasis mine):

Don't create a web API controller by deriving from the Controller class. Controller derives from ControllerBase and adds support for views, so it's for handling web pages, not web API requests. There's an exception to this rule: if you plan to use the same controller for both views and web APIs, derive it from Controller.

I seem to remember that there was no ControllerBase in the first MVC iterations, it was inserted later. Hence the slightly odd naming/inheritance structure.

like image 120
Henk Holterman Avatar answered Sep 22 '22 16:09

Henk Holterman