ControllerBase
Represents a base class for MVC controllers that do not perform view rendering. This class provides core functionality for handling HTTP requests, including action execution, model binding, and authorization.
Description
ControllerBase is a fundamental class in ASP.NET Core MVC for building web APIs.
It offers a lightweight foundation for controllers that primarily return data (like JSON or XML)
rather than rendering HTML views. It includes essential features such as:
- Action execution pipeline management
- Model binding and validation
- Authorization and authentication
- Result execution
- Dependency Injection integration
By inheriting from ControllerBase, you can create controllers that are focused on
handling API endpoints without the overhead of view rendering capabilities, leading to more
efficient and specialized API implementations.
Properties
| Name | Type | Description |
|---|---|---|
ControllerContext |
ControllerContext |
Gets or sets the context information for the controller. |
HttpContext |
HttpContext |
Gets the HttpContext for the current request. |
Request |
HttpRequest |
Gets the HttpRequest for the current request. |
Response |
HttpResponse |
Gets the HttpResponse for the current request. |
ModelState |
ModelStateDictionary |
Gets a dictionary that contains the state of the model binding and validation process. |
Methods
| Name | Signature | Description |
|---|---|---|
Ok |
IActionResult Ok(object value) |
Returns an Ok (200) response with the specified value. |
BadRequest |
IActionResult BadRequest(object error) |
Returns a Bad Request (400) response with the specified error. |
NotFound |
IActionResult NotFound() |
Returns a Not Found (404) response. |
CreatedAtAction |
IActionResult CreatedAtAction(string actionName, object routeValues, object value) |
Returns a Created At Action (201) response. |
Ok |
IActionResult Ok() |
Returns an Ok (200) response. |
C# Example
using Microsoft.AspNetCore.Mvc;
namespace MyApi.Controllers
{
[ApiController]
[Route("api/[controller]")]
public class ProductsController : ControllerBase
{
[HttpGet]
public IActionResult GetAllProducts()
{
// In a real application, you would fetch products from a database
var products = new[]
{
new { Id = 1, Name = "Laptop" },
new { Id = 2, Name = "Keyboard" }
};
return Ok(products); // Returns 200 OK with the product data
}
[HttpPost]
public IActionResult CreateProduct([FromBody] Product newProduct)
{
if (!ModelState.IsValid)
{
return BadRequest(ModelState); // Returns 400 Bad Request if model state is invalid
}
// In a real application, you would save the newProduct to the database
newProduct.Id = 3; // Assign an ID
return CreatedAtAction(nameof(GetProductById), new { id = newProduct.Id }, newProduct); // Returns 201 Created
}
[HttpGet("{id}")]
public IActionResult GetProductById(int id)
{
// In a real application, you would fetch the product from a database
var product = new { Id = id, Name = "Sample Product" };
if (product == null)
{
return NotFound(); // Returns 404 Not Found
}
return Ok(product); // Returns 200 OK
}
}
// A simple model class for demonstration
public class Product
{
public int Id { get; set; }
public string Name { get; set; }
}