ControllerBase Class In ASP.NET Core

Introduction

  • A cross-platform, high-performance, open-source framework for creating contemporary, cloud-based, and internet-connected apps is called ASP.NET Core. The ControllerBase class, which acts as the foundation class for controllers that process HTTP requests, is one of the essential components of an ASP.NET Core application.

What is the ControllerBase class?

  • A foundation class for controllers in ASP.NET Core that manage HTTP requests is called ControllerBase. It offers a collection of typical methods and attributes that controllers may use to process HTTP requests and provide HTTP answers. Instead of implementing any particular behaviour or logic, the ControllerBase class offers a collection of standard methods and attributes that may be used by derived classes.
  • Microsoft.AspNetCore contains the definition of the ControllerBase class.
  • The ASP.NET Core MVC framework includes the MVC namespace. It offers a more straightforward, lightweight solution for creating APIs and microservices and is developed from the Controller class.

Key Features of the ControllerBase Class

  • In ASP.NET Core, the ControllerBase class offers a number of crucial capabilities and functions required for developing online applications and APIs. The following are some of the ControllerBase class’s most crucial characteristics

Action Results

Methods for producing action results that may be sent to the client in response to an HTTP request are provided by the ControllerBase class. These outcomes of action consist of: ViewResult: Produces an HTML answer by rendering a view.

  • ViewResult- Renders a view to generate an HTML response
  • PartialViewResult- Renders a partial view to generate an HTML response
  • JsonResult- Serializes an object to JSON format and returns it as the response
  • ContentResult- Returns a string or content as the response
  • StatusCodeResult- Returns an HTTP status code as the response
  • RedirectResult- Redirects the request to a different URL
  • FileResult- Returns a file as the response
  • ObjectResult- Serializes an object to a specified format (e.g., JSON, XML) and returns it as the response.

Routing

For defining routing for controllers and actions, the ControllerBase class has a number of characteristics and methods. They consist of :

  • RouteAttribute- Used to specify the URL pattern for a controller or action
  • HttpGetAttribute, HttpPostAttribute, HttpPutAttribute, HttpDeleteAttribute- Used to specify the HTTP method for an action
  • RouteData- A property that provides access to the routing data for the current request

Model Binding

Methods are available for linking HTTP request data to action parameters in the ControllerBase class. They consist of :

  • FromQueryAttribute- Binds data from the query string
  • FromRouteAttribute- Binds data from the URL segment
  • FromHeaderAttribute- Binds data from an HTTP header
  • FromBodyAttribute- Binds data from the request body

Example Usage of the ControllerBase class

  • Let’s look at a straightforward example to see how the ControllerBase class might be applied in real-world scenarios. Let’s say that we want to create an API that provides a list of users. The following is an example of how to develop a UserController class that inherits from the ControllerBase class :
using Microsoft.AspNetCore.Mvc;
using System.Collections.Generic;

[ApiController]
[Route("api/[controller]")]
public class UserController: ControllerBase {
    private readonly List < string > users = new List < string > {
        "Lalji Dhameliya"
    };
    [HttpGet]
    public IActionResult GetUsers() {
        return new JsonResult(users);
    }
}

Conclusion

  • In ASP.NET Core, the ControllerBase class serves as a fundamental building block for controllers and offers a wealth of capabilities and functions for processing HTTP requests and producing HTTP answers. Developers may create robust, high-performance web apps and APIs that are simple to maintain by utilising the features of the ControllerBase class.

Submit a Comment

Your email address will not be published. Required fields are marked *

Subscribe

Select Categories