What is ASP.NET Core?
ASP.NET Core is a cross-platform, high-performance framework for building modern, cloud-based, internet-connected applications. It's redesigned from the ground up to be fast, flexible, and modular.
Key Features
- High Performance: One of the fastest web frameworks in TechEmpower benchmarks
- Cross-Platform: Develop and deploy on Windows, Linux, and macOS
- Unified Framework: Build web UI and web APIs with the same tooling
- Built-in DI: Dependency injection is a first-class citizen
Minimal APIs
Create lightweight APIs with minimal code:
var builder = WebApplication.CreateBuilder(args);
var app = builder.Build();
app.MapGet("/", () => "Hello World!");
app.MapGet("/users/{id}", (int id) =>
new User(id, $"User {id}"));
app.MapPost("/users", (User user) =>
Results.Created($"/users/{user.Id}", user));
app.Run();
record User(int Id, string Name);
Controllers and MVC
For larger applications, use controllers:
[ApiController]
[Route("api/[controller]")]
public class ProductsController : ControllerBase
{
private readonly IProductService _service;
public ProductsController(IProductService service)
{
_service = service;
}
[HttpGet]
public async Task<ActionResult<IEnumerable<Product>>> GetAll()
{
var products = await _service.GetAllAsync();
return Ok(products);
}
[HttpGet("{id}")]
public async Task<ActionResult<Product>> GetById(int id)
{
var product = await _service.GetByIdAsync(id);
return product is null ? NotFound() : Ok(product);
}
}
Middleware Pipeline
Configure request processing with middleware:
var builder = WebApplication.CreateBuilder(args);
// Add services
builder.Services.AddControllers();
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
var app = builder.Build();
// Configure middleware
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}
app.UseHttpsRedirection();
app.UseAuthentication();
app.UseAuthorization();
app.MapControllers();
app.Run();
Getting Started
- Create a new web API:
dotnet new webapi -n MyApi - Add Entity Framework Core for data access
- Implement authentication with Identity or JWT
- Deploy to Azure App Service or containers