Skip to main content
C# Fundamentals for Beginners to Advanced
CHAPTER 26 Beginner

ASP.NET Core Basics

Updated: May 17, 2026
5 min read

# CHAPTER 26

ASP.NET Core Basics

1. Introduction

So far, you have built Console Applications (black terminal windows). But in the real world, most software runs on the Web. ASP.NET Core is Microsoft's massive, high-performance, open-source framework for building web applications and REST APIs using C#. It powers some of the largest websites on the internet.

2. Learning Objectives

By the end of this chapter, you will be able to:
  • Understand what ASP.NET Core is.
  • Create an ASP.NET Core Web API project.
  • Understand the MVC (Model-View-Controller) architecture.
  • Write a Controller to handle HTTP requests.
  • Understand Routing.

3. What is ASP.NET Core?

ASP.NET Core sits on top of the .NET runtime. It provides everything needed to listen for HTTP requests from web browsers (like Chrome or Safari), process the data, and return a response (like HTML pages or JSON data).

There are two main ways to use it:

  1. 1. Web API: Returns JSON data. Used as the backend for React/Angular apps or Mobile apps.
  1. 2. MVC / Razor Pages: Returns full HTML pages directly to the browser.

4. Creating a Web API Project

Let's build a simple backend API. Open your terminal and run:
bash
123
dotnet new webapi -n MyBackendApp
cd MyBackendApp
dotnet run

The application will start a local web server (e.g., http://localhost:5000).

5. The MVC Architecture

Most web frameworks use the Model-View-Controller pattern:
  • Model: Represents the data (e.g., a C# User class).
  • View: The User Interface (HTML/CSS). *Not used in Web APIs.*
  • Controller: The brain. It receives the HTTP request, grabs the Model data, and returns the response.

6. Writing a Controller

In an ASP.NET Web API, a Controller is simply a C# class that inherits from ControllerBase. We use Attributes (text in [brackets]) to define the HTTP methods (GET, POST) and Routing.
csharp
12345678910111213141516171819202122232425262728
using Microsoft.AspNetCore.Mvc;
using System.Collections.Generic;

namespace MyBackendApp.Controllers
{
    // Routing: This controller answers requests sent to "http://localhost:5000/api/users"
    [ApiController]
    [Route("api/[controller]")]
    public class UsersController : ControllerBase
    {
        // Answers a standard GET request
        [HttpGet]
        public IActionResult GetAllUsers()
        {
            var users = new List<string> { "Alice", "Bob", "Charlie" };
            
            // Automatically converts the C# List into a JSON response!
            return Ok(users); 
        }

        // Answers a GET request with an ID parameter in the URL (e.g., /api/users/5)
        [HttpGet("{id}")]
        public IActionResult GetUserById(int id)
        {
            return Ok($"You requested user ID: {id}");
        }
    }
}

7. Routing Explained

Routing is how the framework maps a URL (typed in the browser) to a specific C# method.
  • [Route("api/[controller]")] tells ASP.NET to take the class name (UsersController), drop the word "Controller", and use api/Users as the base URL.
  • [HttpGet("{id}")] adds to the route. If the browser asks for /api/users/99, ASP.NET finds this method and passes 99 into the id parameter automatically!

8. Modern C# Minimal APIs

In .NET 6+, Microsoft introduced Minimal APIs. Instead of creating large Controller classes, you can map routes directly in the Program.cs file using lambda expressions.
Program.cs
1234567
var builder = WebApplication.CreateBuilder(args);
var app = builder.Build();

// A Minimal API Endpoint
app.MapGet("/api/hello", () => "Hello from Minimal API!");

app.Run();

9. Common Mistakes

  • Forgetting [ApiController]: If you forget this attribute on a controller class, automatic JSON validation and routing behaviors will fail.
  • Port Conflicts: If dotnet run fails, your port (e.g., 5000) might be in use by another application.

10. Best Practices

  • Keep Controllers thin. Controllers should only handle receiving the request and returning the response. Complex business logic and database code should be moved to separate "Service" classes.

11. Exercises

  1. 1. Create a dotnet new webapi project.
  1. 2. Inside Program.cs, create a Minimal API endpoint app.MapGet("/api/time", () => DateTime.Now.ToString());.
  1. 3. Run the project and visit the URL in your browser to see the live server time.

12. MCQs with Answers

Q1. What is ASP.NET Core? a) A database b) A cross-platform framework for building web apps and APIs using C# c) A video game engine Answer: b) A cross-platform framework for building web apps and APIs using C#
Question 2

What terminal command creates a new ASP.NET Core Web API project?

Question 3

In the MVC pattern, what does the Controller do?

Question 4

What is a Web API designed to return to the client?

Question 5

Which attribute must be placed above a class to make it a modern API Controller?

Q6. What does the [HttpGet] attribute do? a) Connects to a database b) Maps an HTTP GET request to the specific C# method below it c) Secures the method Answer: b) Maps an HTTP GET request to the specific C# method below it
Question 7

If a controller has [Route("api/[controller]")] and is named ProductsController, what is its base URL path?

Question 8

What does return Ok(data); do in a Controller?

Question 9

What is a Minimal API?

Question 10

How does the ASP.NET framework match a URL to a C# method?

13. Interview Questions

  • Q: Explain the MVC architecture as it applies to ASP.NET Core.
  • Q: What is the difference between an ASP.NET Web API and an ASP.NET MVC/Razor Pages application?
  • Q: Explain how Dependency Injection works in the context of ASP.NET Core controllers.

14. Summary

ASP.NET Core takes your C# knowledge to the web. By utilizing Controllers and Routing attributes, you can easily build powerful REST APIs that serve JSON data to front-end frameworks like React, or mobile apps built with MAUI/Flutter.

15. Next Chapter Recommendation

In Chapter 25 we wrote raw SQL. In Chapter 26 we built a web API. In Chapter 27: Entity Framework Core Basics, we will combine both concepts using an ORM to interact with databases without ever writing a single line of SQL!

Finish this Chapter

Save your progress on your learning path and prepare for coding interview challenges.

Discussion

Join the discussion

Log in or create a free account to participate.

Sort: ·