ASP.NET Core Fundamentals
Overview
ASP.NET Core is Microsoft's cross‑platform framework for building modern cloud‑based applications. This tutorial covers the essentials: project setup, building REST APIs with controllers, Entity Framework Core for data access, and dependency injection.
1. Project Setup
Use the .NET CLI to create a project:
Project structure:
Controllers/– API controllersModels/– entity modelsData/– DbContext and repositoriesProgram.cs– application entry pointappsettings.json– configuration
2. Controllers & Routing
Use [ApiController] and [Route] attributes.
Attributes: [HttpGet], [HttpPost], [HttpPut],
[HttpDelete], [FromBody], [FromQuery].
3. Entity Framework Core
EF Core is the ORM for .NET. Use code‑first approach.
Migrations
4. Dependency Injection
ASP.NET Core has built‑in DI. Register services in Program.cs.
Service lifetimes: AddTransient, AddScoped, AddSingleton.
Quiz
Question 1
Which attribute marks a controller for API endpoints?
- [Controller]
- [ApiController]
- [Route]
- [WebApi]
Show answer
Question 2
Which command creates a new database migration in EF Core?
- dotnet ef migration add
- dotnet ef migrations add
- dotnet ef update
- dotnet ef db
Show answer
Question 3
What is the default lifetime of a service registered with
AddScoped?
- Singleton
- Transient
- Scoped (per request)
- Per method
Show answer
Exercises
Exercise 1
Create a Category model with Id, Name, Description. Add a DbSet
to the DbContext.
Sample answer
Exercise 2
Add a PUT /api/products/{id} endpoint that updates an existing
product.
Sample answer
Homework
Homework 1
Build a complete CRUD API for Order with fields: Id,
CustomerName, OrderDate, TotalAmount. Use EF Core and a service layer.
Sample outline
- Model: Order (Id, CustomerName, OrderDate, TotalAmount)
- DbContext: Orders DbSet
- Service: IOrderService with CRUD methods
- Controller: OrdersController with all endpoints
- Migrations: Add and update database
Mini‑Project
Inventory Management API
Build an Inventory Management API with:
- Product: Id, Name, SKU, Price, Stock
- Category: Id, Name
- Relationship: Product belongs to Category
- CRUD for both
- Search endpoint: GET /api/products?search=...&category=...
Sample outline
- Models: Product, Category
- DbContext: Configure relationships using Fluent API or attributes
- Services: ProductService with search functionality
- Controllers: ProductController, CategoryController
- Migrations: Add and apply
Tutorial Summary
You learned the fundamentals of ASP.NET Core: project setup, building REST APIs with controllers, Entity Framework Core for data persistence, and dependency injection. You built a solid foundation for enterprise .NET development.
Key takeaway: ASP.NET Core is a modern, high‑performance framework with excellent tooling. Its dependency injection and ORM make it a joy to build maintainable applications.