Extending controllers

As already mentioned, controllers are classes, therefore they can extend other types, including other controllers. This technique can be applied so that we can reuse a particular implementation or feature. In general, the controllers extend the Controller or ControllerBase classes, which are part of the ASP.NET Core framework. These base classes provide some utilities to manage requests and responses on behalf of the controller. First of all, let's analyze the difference between the Controller and ControllerBase classes:

  • ControllerBase represents a base class for an MVC controller without view support. It provides some essential attributes to child classes, such as the HttpContextRequest, and Response attributes.
  • The Controller class extends the ControllerBase class, but it also adds some properties and methods for managing views, such as the ViewData attribute and the View() and PartialView() methods.

When we deal with RESTful APIs and with web services in general, the ControllerBase class provides sufficient utilities. If we are dealing with views, however, we should extend the Controller class.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.93.221