Last Updated: November 21, 2025
ASP.NET Core
Cross-platform .NET framework
Core Concepts
| Item | Description |
|---|---|
Controller
|
Handles HTTP requests |
Middleware
|
Request/response pipeline |
Dependency Injection
|
Built-in DI container |
Model Binding
|
Auto-map request data to objects |
Routing
|
URL pattern matching |
Common CLI Commands
dotnet new webapi -n MyApi
Create new Web API project
dotnet run
Run the application
dotnet build
Build the project
dotnet test
Run tests
dotnet add package EntityFramework
Add NuGet package
API Controller Example
[ApiController]
[Route("api/[controller]")]
public class UsersController : ControllerBase
{
private readonly IUserService _userService;
public UsersController(IUserService userService)
{
_userService = userService;
}
[HttpGet("{id}")]
public async Task<ActionResult<User>> GetUser(int id)
{
var user = await _userService.GetByIdAsync(id);
return user == null ? NotFound() : Ok(user);
}
[HttpPost]
public async Task<ActionResult<User>> CreateUser(User user)
{
await _userService.CreateAsync(user);
return CreatedAtAction(nameof(GetUser), new { id = user.Id }, user);
}
}
Best Practices
- Use async/await for I/O operations
- Implement proper error handling middleware
- Use DTOs and AutoMapper for object mapping
- Follow RESTful API design principles
💡 Pro Tips
Quick Reference
Use Entity Framework Core for database operations