ASP.NET Core API Reference
Comprehensive documentation for ASP.NET Core API development.
Controllers
Controllers are classes that handle incoming HTTP requests and return responses. They are the core components of ASP.NET Core MVC and API applications.
public
class
ControllerBase
The base class for controllers that do not support views. Ideal for API controllers.
Microsoft.AspNetCore.Mvc
Inherits from:
object
public
class
Controller
: ControllerBase
The base class for controllers that support views (MVC). Can also be used for APIs but ControllerBase
is preferred for simplicity.
Microsoft.AspNetCore.Mvc
Inherits from:
ControllerBase
Action Methods
Methods within a controller that are executed in response to a specific HTTP request.
public
IActionResult
Index
()
Handles GET requests for the default action of a controller.
OkResult
, NotFoundResult
, JsonResult
, etc.
public
IActionResult
Create
( MyModel
model
)
Handles POST requests, typically used for creating new resources.
MyModel model
- The data to create the resource with.
Returns:
IActionResult
public
IActionResult
Update
( int
id
, MyModel
model
)
Handles PUT requests, typically used for updating existing resources.
int id
, MyModel model
Returns:
IActionResult
public
IActionResult
Delete
( int
id
)
Handles DELETE requests, typically used for removing resources.
int id
Returns:
IActionResult
Routing
Defines how incoming requests are mapped to specific action methods.
[Route("api/[controller]")]
public
ControllerBase
Attribute-based routing applied at the controller level. [controller]
is a token replaced by the controller name without the suffix.
[HttpGet("{id}")]
public
IActionResult
GetItem
( int
id
)
Attribute-based routing applied at the action method level. Defines a GET request for a specific ID.
[HttpGet]
public
IEnumerable<MyModel>
GetAll
()
Attribute-based routing for GET requests to the controller's base path.
Middleware
Components that process HTTP requests and responses in a pipeline.
Configured in Startup.cs
(or Program.cs
in .NET 6+).
app.UseRouting();
Enables routing.
app.UseAuthorization();
Enables authorization.
app.UseEndpoints(endpoints => { endpoints.MapControllers(); });
Maps incoming requests to controller actions.
Dependency Injection
A design pattern where dependencies are provided to a class rather than the class creating them itself. ASP.NET Core has built-in DI support.
public
class
MyService
A service class that can be injected.
private readonly IMyService _myService;
public
MyController
( IMyService
myService
)
Constructor injection of a service into a controller.
Startup.cs
(or Program.cs
) using services.AddScoped<IMyService, MyService>();
or similar.
Models
Plain Old CLR Objects (POCOs) that represent data structures.
public
class
Product
{
public
int
Id
{ get;
set;
}
public
string
Name
{ get;
set;
}
public
decimal
Price
{ get;
set;
}
}
A simple model representing a product.
YourApp.Models
ViewModels
Data transfer objects specifically designed for presentation, often combining data from multiple models.
public
class
ProductDetailViewModel
{
public
string
ProductName
{ get;
set;
}
public
string
Description
{ get;
set;
}
public
decimal
CurrentPrice
{ get;
set;
}
public
int
StockCount
{ get;
set;
}
}
A ViewModel for displaying detailed product information.
Filters
Allow for cross-cutting concerns like authentication, authorization, logging, and caching.
public
class
AuthorizeAttribute
: IFilterFactory
, IOrderedFilter
An example of an action filter for authorization.
public
class
MyLoggingFilter
: IActionFilter
{
public
void
OnActionExecuting
( ActionExecutingContext
context
) { /* ... */ }
public
void
OnActionExecuted
( ActionExecutedContext
context
) { /* ... */ }
}
A custom action filter for logging.
Services
Business logic components that can be reused across controllers and other parts of the application.
public
interface
IProductService
{
Task<IEnumerable<Product>>
GetAllProductsAsync
();
Task<Product>
GetProductByIdAsync
( int
id
);
}
Interface for a product service.
public
class
ProductService
: IProductService
{
public
async
Task<IEnumerable<Product>>
GetAllProductsAsync
() { /* ... */ }
public
async
Task<Product>
GetProductByIdAsync
( int
id
) { /* ... */ }
}
Implementation of the product service.
Endpoints
Minimal APIs provide a way to build HTTP APIs with minimal boilerplate code.
var builder = WebApplication.CreateBuilder(args);
var app = builder.Build();
app.MapGet("/hello", () => "Hello World!");
A simple Minimal API endpoint.
app.MapGet("/products/{id}", async (int id, IProductService productService) =>
{
var
product
= await
productService.GetProductByIdAsync
(id);
return
product
== null
? Results.NotFound() : Results.Ok(product);
});
A Minimal API endpoint with parameter binding and dependency injection.
Razor Pages
A page-focused programming model for ASP.NET Core that makes it easy to build rich UI and work with APIs.
public
class
IndexModel
: PageModel
{
public
string
Message
{ get;
set;
}
public
void
OnGet
()
{
Message
= "Welcome to Razor Pages!";
}
}
A sample Razor PageModel with a `OnGet` handler.
Often used for serving HTML UIs, but can also serve API responses if needed.
MVC Core
The Model-View-Controller architectural pattern applied to ASP.NET Core, allowing for structured development of web applications and APIs.
ASP.NET Core MVC provides a robust framework for building complex web applications with clear separation of concerns.