MSDN Documentation

Microsoft Developer Network

API Controllers in ASP.NET Core

API controllers are a fundamental part of building web APIs with ASP.NET Core. They handle incoming HTTP requests and formulate HTTP responses.

What is an API Controller?

An API controller is a type of controller that is specifically designed to return data, typically in JSON or XML format, rather than HTML views. In ASP.NET Core, API controllers inherit from ControllerBase (or Controller if you need support for views and model validation, although this is less common for pure APIs).

Key Features and Concepts

Creating a Simple API Controller

Let's create a basic API controller that returns a list of items.

Example: ProductsController.cs

using Microsoft.AspNetCore.Mvc;
using System.Collections.Generic;
using YourApp.Models; // Assuming you have a Product model

[ApiController]
[Route("api/[controller]")]
public class ProductsController : ControllerBase
{
    private readonly List<Product> _products = new List<Product>
    {
        new Product { Id = 1, Name = "Laptop", Price = 1200.00m },
        new Product { Id = 2, Name = "Keyboard", Price = 75.50m },
        new Product { Id = 3, Name = "Mouse", Price = 25.00m }
    };

    [HttpGet]
    public ActionResult<IEnumerable<Product>> Get()
    {
        return Ok(_products);
    }

    [HttpGet("{id}", Name = "GetProductById")]
    public ActionResult<Product> Get(int id)
    {
        var product = _products.Find(p => p.Id == id);
        if (product == null)
        {
            return NotFound();
        }
        return Ok(product);
    }

    [HttpPost]
    public ActionResult<Product> Post([FromBody] Product newProduct)
    {
        if (newProduct == null)
        {
            return BadRequest();
        }

        // Assign a new ID (in a real app, this would come from a database)
        newProduct.Id = _products.Count > 0 ? _products.Max(p => p.Id) + 1 : 1;
        _products.Add(newProduct);

        return CreatedAtRoute("GetProductById", new { id = newProduct.Id }, newProduct);
    }
}

// Assuming a simple Product model:
public class Product
{
    public int Id { get; set; }
    public string Name { get; set; }
    public decimal Price { get; set; }
}

The [ApiController] Attribute

Applying the [ApiController] attribute to a controller class enables several project-wide API behaviors:

Routing with [Route]

The [Route] attribute defines the base URL path for the controller's actions. Using the pattern "api/[controller]" is a common convention:

So, a request to /api/products would target this controller.

Routes on action methods can further refine this, for example, [HttpGet("{id}")] creates a route like /api/products/{id}.

Action Results

Action methods typically return types that derive from ActionResult. This provides a way to return different HTTP status codes and response bodies. Common examples include:

Note on ControllerBase vs. Controller

For pure API projects, inheriting from ControllerBase is recommended as it has fewer dependencies than Controller, which includes MVC view support.

Tip for API Development

Always define clear API contracts. Use DTOs (Data Transfer Objects) to shape the data returned from your API and avoid exposing internal domain models directly.

Further Reading