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. Web API: Returns JSON data. Used as the backend for React/Angular apps or Mobile apps.
- 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
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#
Userclass).
- 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 fromControllerBase. We use Attributes (text in [brackets]) to define the HTTP methods (GET, POST) and Routing.
csharp
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 useapi/Usersas the base URL.
-
[HttpGet("{id}")]adds to the route. If the browser asks for/api/users/99, ASP.NET finds this method and passes99into theidparameter 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 theProgram.cs file using lambda expressions.
Program.cs
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 runfails, 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.
Create a
dotnet new webapiproject.
-
2.
Inside
Program.cs, create a Minimal API endpointapp.MapGet("/api/time", () => DateTime.Now.ToString());.
- 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?
[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.