Track E · Tutorial 1

ASP.NET Core Fundamentals

Chapter 28 · Backend Specialization
~3 hours Intermediate C# · Web API · EF Core · DI

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.

Why this matters: ASP.NET Core is a first‑class framework for building high‑performance, secure, and scalable APIs. It's widely used in enterprise and cloud environments.

1. Project Setup

Use the .NET CLI to create a project:

dotnet new webapi -n MyApi cd MyApi dotnet run

Project structure:

  • Controllers/ – API controllers
  • Models/ – entity models
  • Data/ – DbContext and repositories
  • Program.cs – application entry point
  • appsettings.json – configuration

2. Controllers & Routing

Use [ApiController] and [Route] attributes.

// Controllers/ProductsController.cs [ApiController] [Route("api/[controller]")] public class ProductsController : ControllerBase { [HttpGet] public IActionResult GetAll() { return Ok(new[] { new { Id = 1, Name = "Laptop" } }); } [HttpGet("{id}")] public IActionResult GetById(int id) { return Ok(new { Id = id, Name = "Laptop" }); } [HttpPost] public IActionResult Create([FromBody] Product product) { return CreatedAtAction(nameof(GetById), new { id = product.Id }, product); } }

Attributes: [HttpGet], [HttpPost], [HttpPut], [HttpDelete], [FromBody], [FromQuery].

3. Entity Framework Core

EF Core is the ORM for .NET. Use code‑first approach.

dotnet add package Microsoft.EntityFrameworkCore.SqlServer dotnet add package Microsoft.EntityFrameworkCore.Tools
// Models/Product.cs public class Product { public int Id { get; set; } public string Name { get; set; } public decimal Price { get; set; } } // Data/AppDbContext.cs public class AppDbContext : DbContext { public AppDbContext(DbContextOptions options) : base(options) { } public DbSet Products { get; set; } }
// Program.cs – register DbContext builder.Services.AddDbContext(options => options.UseSqlServer(builder.Configuration.GetConnectionString("DefaultConnection")));

Migrations

dotnet ef migrations add InitialCreate dotnet ef database update

4. Dependency Injection

ASP.NET Core has built‑in DI. Register services in Program.cs.

// Register a service builder.Services.AddScoped(); // Use in controller public class ProductsController : ControllerBase { private readonly IProductService _productService; public ProductsController(IProductService productService) { _productService = productService; } }

Service lifetimes: AddTransient, AddScoped, AddSingleton.

Quiz

Question 1

Which attribute marks a controller for API endpoints?

  • [Controller]
  • [ApiController]
  • [Route]
  • [WebApi]
Show answer
B. [ApiController].

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
B. dotnet ef migrations add.

Question 3

What is the default lifetime of a service registered with AddScoped?

  • Singleton
  • Transient
  • Scoped (per request)
  • Per method
Show answer
C. Scoped (per request).

Exercises

Exercise 1

Create a Category model with Id, Name, Description. Add a DbSet to the DbContext.

Sample answer
public class Category { public int Id { get; set; } public string Name { get; set; } public string Description { get; set; } } // In AppDbContext: public DbSet Categories { get; set; }

Exercise 2

Add a PUT /api/products/{id} endpoint that updates an existing product.

Sample answer
[HttpPut("{id}")] public IActionResult Update(int id, [FromBody] Product product) { if (id != product.Id) return BadRequest(); // Update logic return NoContent(); }

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.