Pro EP 35 : Controller vs ControllerBase in C#

Muhammad Waseem
Become .NET Pro !
2 min readApr 6, 2023

💡 Do you know the difference b/w Controller and ControllerBase in C#

To create a controller class in C#, we need to inherit it from either the Controller or ControllerBase.

Controller:
1) Abstract class that inherits from ControllerBase
2) It has access to all methods of ControllerBase with addition
3) Comes with facility of action filters e.g. OnActionExecuting and OnActionExecuted
4) Don’t use this for Web API but if you want to use same controller for views and API’s then fine
5) If you want action filter, json helpers, data passing b/w actions or rendering methods then then use it over other.

ControllerBase:
1) It implements IController
2) For Web API you should use this because we don’t need methods related to views.
3) It provides properties and methods that are useful for handling HTTP requests.
4) Let me remind you some methods of ControllerBase e.g., BadRequest and NotFound
5) If you are only concerned about HTTP Requests or Model States then this should ne the choice

If you want to help the author in growing

  1. Subscribe my Weekly .NET Newsletter of C#/.NET with 1100+ Software Engineers.
  2. Become Patron to get access to 100+ Compiled Questions and Answers at one place.
  3. Download my eBook at Gum road that contains 30+ .NET Tips.

--

--