⚙️ ASP.NET Core Fundamentals: Middleware, Routing & Controllers

Samim.Hossain
Samim Hossain
Published on Jan, 18 2026 2 min read 0 comments
Text Image

Introduction

Now that you understand C# fundamentals, it’s time to see how .NET handles web requests.

ASP.NET Core is a high-performance, cross-platform web framework used to build:

  • REST APIs
  • Web applications
  • Microservices

In this article, you’ll learn:

  • How ASP.NET Core processes requests
  • What middleware is
  • How routing works
  • How controllers handle HTTP requests

This is Week 4 of our .NET weekly blog series.

What Is ASP.NET Core?

ASP.NET Core is a modern web framework built on top of .NET.

Key Features

  • Cross-platform
  • Fast & lightweight
  • Built-in Dependency Injection
  • Cloud & container friendly

It replaces the older ASP.NET Framework.

Understanding the Request Pipeline

Every HTTP request in ASP.NET Core goes through a pipeline.

Request Flow

Client → Middleware → Routing → Controller → Response

Each component decides whether to:

  • Process the request
  • Modify it
  • Pass it to the next component

Middleware Explained

Middleware is software that:

  • Handles requests and responses
  • Runs in sequence
  • Can stop or forward requests

Common Middleware Examples

  • Authentication
  • Logging
  • Exception handling
  • CORS

Creating Custom Middleware

public class RequestLoggingMiddleware
{
    private readonly RequestDelegate _next;

    public RequestLoggingMiddleware(RequestDelegate next)
    {
        _next = next;
    }

    public async Task Invoke(HttpContext context)
    {
        Console.WriteLine(context.Request.Path);
        await _next(context);
    }
}

Registering middleware:

app.UseMiddleware<RequestLoggingMiddleware>();

Order matters in middleware registration.

Built-in Middleware Order (Important)

app.UseExceptionHandler();
app.UseHttpsRedirection();
app.UseRouting();
app.UseAuthentication();
app.UseAuthorization();
app.MapControllers();

Wrong order = bugs 😄

Routing in ASP.NET Core

Routing maps a URL to a controller action.

Attribute Routing Example

[ApiController]
[Route("api/users")]
public class UsersController : ControllerBase
{
    [HttpGet]
    public IActionResult GetUsers()
    {
        return Ok("User list");
    }
}

Access URL:

GET /api/users

Route Parameters

[HttpGet("{id}")]
public IActionResult GetUser(int id)
{
    return Ok($"User ID: {id}");
}

URL:

GET /api/users/5

Controllers Explained

Controllers:

  • Receive HTTP requests
  • Process business logic
  • Return responses

Types of Controllers

| Type           | Use Case  |
| -------------- | --------- |
| MVC Controller | Web pages |
| API Controller | REST APIs |

API Controller Example

[ApiController]
[Route("api/products")]
public class ProductsController : ControllerBase
{
    [HttpPost]
    public IActionResult Create()
    {
        return Ok("Product created");
    }
}

Action Results

ASP.NET Core provides built-in response helpers:

return Ok(data);        // 200
return NotFound();      // 404
return BadRequest();    // 400
return Unauthorized();  // 401

Using proper status codes is best practice.

Minimal APIs (Quick Intro)

ASP.NET Core also supports Minimal APIs.

app.MapGet("/hello", () => "Hello World");

✔ Lightweight
✔ Less boilerplate
❌ Not ideal for large projects

Common Beginner Mistakes

❌ Confusing middleware with controllers
❌ Wrong middleware order
❌ Hardcoding routes
❌ Not using proper HTTP verbs

Best Practices

✅ Keep middleware small
✅ Use attribute routing
✅ Separate concerns
✅ Return meaningful HTTP codes

What You’ve Learned

✔ ASP.NET Core basics
✔ Request pipeline
✔ Middleware
✔ Routing
✔ Controllers

You are now officially inside real backend development with .NET 🚀

0 Comments