Log In

Don't have an account? Sign up now

Lost Password?

Sign Up

Prev Next

Module 8: ASP.NET Core Fundamentals

This module is where your C# logic meets the internet. ASP.NET Core is Microsoft’s modern, cross-platform, high-performance framework for building cloud-based, internet-connected applications.

1. What is ASP.NET Core?

ASP.NET Core is a complete redesign of the older ASP.NET 4.x. It is built to be fast, modular, and run anywhere (Windows, macOS, Linux).

  • Cross-platform: You can develop and run it on Docker, Linux, or Mac.
  • High Performance: It consistently ranks as one of the fastest web frameworks in the TechEmpower benchmarks.
  • Unified: It merges the previous “MVC” and “Web API” frameworks into a single programming model.

2. ASP.NET Core vs. Classic ASP.NET MVC

If you see tutorials from before 2016, they likely refer to “Classic” ASP.NET. Here is how they differ:

FeatureASP.NET MVC (Classic)ASP.NET Core
HostingRequires IIS (Windows Only)Self-hosted (Kestrel) or IIS, Nginx, Apache
DependenciesIncluded in the .NET FrameworkShipped as NuGet packages (Modular)
ConfigurationWeb.config (XML)appsettings.json (JSON)
Dependency InjectionRequires 3rd party (Autofac, Ninject)Built-in by default

3. Project Structure & Lifecycle

When you create a new ASP.NET Core Web API or Web App, the structure looks like this:

  • Program.cs: The entry point. It sets up the Web Host, configures services (DI), and defines the request pipeline.
  • appsettings.json: Where you store connection strings and environment-specific settings.
  • wwwroot: (For Web Apps) Stores static files like CSS, JS, and images.
  • Controllers/: Where the logic for handling HTTP requests lives.

The Lifecycle:

  1. Request Starts: A user hits a URL.
  2. Kestrel Web Server: Picks up the request.
  3. Middleware Pipeline: Processes the request (Security, Logging, etc.).
  4. Routing: Matches the URL to a specific Controller.
  5. Action Execution: The code runs.
  6. Response: Sent back through the middleware to the user.

4. Middleware & The Request Pipeline

Middleware are software components assembled into an application pipeline to handle requests and responses.

  • Order Matters: If you put “Authorization” before “Authentication,” the app will crash because it doesn’t know who the user is before checking their permissions.
  • Common Middleware: UseRouting(), UseAuthentication(), UseStaticFiles(), and UseExceptionHandler().

5. Routing: Conventional vs. Attribute

Routing is how the framework maps an incoming URL (e.g., /products/5) to a piece of code.

Attribute Routing (Recommended for APIs)

You place attributes directly on the Controller or Action.

C#

[ApiController]
[Route("api/[controller]")] // Routes to /api/products
public class ProductsController : ControllerBase {
    [HttpGet("{id}")] // Routes to /api/products/5
    public IActionResult GetProduct(int id) { ... }
}

Conventional Routing (Common in MVC)

Defined globally in Program.cs. It follows a pattern like {controller=Home}/{action=Index}/{id?}.


6. Controllers & Actions

  • Controller: A class that inherits from ControllerBase (for APIs) or Controller (for Web Apps with Views).
  • Action: A public method inside a controller that returns an IActionResult.
    • Ok() -> Returns 200 Success.
    • NotFound() -> Returns 404.
    • BadRequest() -> Returns 400.

7. Model Binding & Validation

This is the “magic” that takes data from the URL, Form, or JSON Body and turns it into a C# object.

  • Model Binding: If a user sends a JSON {"Name": "Laptop"} to an action expecting a Product object, ASP.NET Core automatically maps the fields.
  • Validation: You can use Data Annotations to enforce rules.C#public class Product { [Required] [StringLength(50)] public string Name { get; set; } [Range(1, 1000)] public decimal Price { get; set; } } If the user sends a price of -5, the framework can automatically return a 400 Bad Request before your code even runs.

Outcome: Build your first web application

By the end of this module, you should be able to:

  1. Create a “Hello World” API.
  2. Create a Controller that accepts a custom Object.
  3. Use Postman or Swagger to send data to your app and see the JSON response.

Leave a Comment

    🚀 Join Common Jobs Pro — Referrals & Profile Visibility Join Now ×
    🔥