C# ASP.NET Core Cheat Sheet
Covers building ASP.NET Core Web APIs with controllers and minimal APIs, the middleware pipeline, routing, and model binding.
3 PagesIntermediateMar 25, 2026
API Controller
A typical RESTful controller with attribute routing.
csharp
[ApiController][Route("api/[controller]")]public class ProductsController : ControllerBase{ private readonly IProductService _service; public ProductsController(IProductService service) => _service = service; [HttpGet] public async Task<ActionResult<List<Product>>> GetAll() => Ok(await _service.GetAllAsync()); [HttpGet("{id:int}")] public async Task<ActionResult<Product>> GetById(int id) { var product = await _service.GetByIdAsync(id); return product is null ? NotFound() : Ok(product); } [HttpPost] public async Task<ActionResult<Product>> Create(Product product) { await _service.AddAsync(product); return CreatedAtAction(nameof(GetById), new { id = product.Id }, product); }}
Middleware Pipeline
Built-in and custom request/response middleware.
csharp
var app = builder.Build();app.UseHttpsRedirection();app.UseRouting();app.UseAuthentication();app.UseAuthorization();app.UseCors("AllowAll");app.MapControllers();app.Run();// Custom middlewareapp.Use(async (context, next) =>{ Console.WriteLine($"Request: {context.Request.Path}"); await next(context); // Call the next middleware in the pipeline});
Minimal API Endpoints
Defining routes without a full controller class.
csharp
var app = builder.Build();app.MapGet("/products/{id}", async (int id, IProductService svc) =>{ var p = await svc.GetByIdAsync(id); return p is not null ? Results.Ok(p) : Results.NotFound();});app.MapPost("/products", async (Product p, IProductService svc) =>{ await svc.AddAsync(p); return Results.Created($"/products/{p.Id}", p);});app.Run();
Core Building Blocks
Concepts that show up in every ASP.NET Core app.
- [ApiController]- Enables automatic model validation and binding-source inference for API controllers
- Routing- Attribute routing ([Route], [HttpGet]) or conventional routing via MapControllerRoute
- Model binding- Automatically maps route, query, and body values to action method parameters
- Middleware order- Executes top-to-bottom for requests and bottom-to-top for responses (a pipeline)
- Filters- [Authorize], action filters, and exception filters run around action execution
- IActionResult- Ok(), NotFound(), BadRequest(), CreatedAtAction() return the correct HTTP status codes
- Options pattern- services.Configure<T>(config.GetSection("Section")) binds config to a strongly-typed class
Pro Tip
Register middleware order carefully — UseAuthentication() must come before UseAuthorization(), and both must come before MapControllers(), or auth checks silently won't apply.
Was this cheat sheet helpful?
Explore Topics
#CASPNETCore#CASPNETCoreCheatSheet#Programming#Intermediate#APIController#MiddlewarePipeline#MinimalAPIEndpoints#CoreBuildingBlocks#WebDevelopment#APIs#DevOps#CheatSheet#SkillVeris
Advertisement
Sri Hayavadhana Info-Tech
Professional Web Designing Services
- Responsive Websites
- E-commerce Solutions
- SEO Friendly Design
- Fast & Secure
- Support & Maintenance