Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

956
Vistas
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?

over 4 years ago · Santiago Trujillo
4 Respuestas
Responde la pregunta

0

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.

over 4 years ago · Santiago Trujillo Denunciar

0

One other important difference is that ControllerBase is abstract class that's why it doesn't implement Dispose method. So you need to handle yourself. see this SO entry

over 4 years ago · Santiago Trujillo Denunciar

0

FROM https://docs.microsoft.com/en-us/aspnet/core/web-api/?view=aspnetcore-3.1

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.

over 4 years ago · Santiago Trujillo Denunciar

0

ControllerBase abstract class

Controller abstract class derives from ControllerBase abstract class therefore supports to Create views ,API Not Advice to Create Views .

ControllerBase abstract class has Member[Properties,Methods] without Implementaion Like

public abstract class ControllerBase{
        public HttpResponse Response { get; }
        public HttpRequest Request { get; }
        public HttpContext HttpContext { get; }
        public virtual RedirectToActionResult RedirectToAction(string actionName);}

ControllerBase has all Request and Response between Client-side and Server-side


Controller abstract class

Controller abstract class Enables us To Create Views to display Data in client-side, it derives from ControllerBase abstract class.

public abstract class Controller : ControllerBase{
        public dynamic ViewBag { get; }
        
        public ViewDataDictionary ViewData { get; set; }
       
        public ITempDataDictionary TempData { get; set; }
}

https://newbedev.com/why-derive-from-controllerbase-vs-controller-for-asp-net-core-web-api

over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda