Welcome to the next step in our journey of building a personal tutor service with ASP.NET Core Web API. In the previous lesson, we focused on the TutorController
, which manages tutoring sessions and handles student queries by interacting with both the model and service layers. Now, we will take a significant step forward by creating a RESTful API for our personal tutor service using ASP.NET Core
. We'll start by setting up the main ASP.NET Core Web API
application, then adapt the TutorController
to integrate with ASP.NET Core
's session management.
Note, that session-based state is used instead of stateless authentication like JWTs because it allows the server to easily manage user-specific data (such as tutoring sessions and conversation history) without requiring the client to handle or resend this information with every request. This simplifies state management for applications that need to track ongoing user interactions.
RESTful APIs are a way for different software systems to communicate over the internet using standard HTTP methods such as GET
, POST
, PUT
, and DELETE
. They provide a set of rules that allow programs to exchange data in a structured and predictable way, typically using JSON
as the data format.
ASP.NET Core Web API is a modern, high-performance framework for building RESTful APIs with C#
. It provides a robust set of tools for routing, model binding, validation, dependency injection, and more. With ASP.NET Core
, you can easily expose your application's functionality as a web API that can be consumed by web clients, mobile apps, or other services.
To get started, create a new ASP.NET Core Web API
project using the .NET CLI
:
This command creates a new project named PersonalTutor
with the necessary structure for a Web API application.
Configuring Services and Session Management
Open the Program.cs
file. First, configure the required services and session management:
Next, build the app, enable session middleware, and define the main routes.
Index Route
The index route (/
) ensures a user session is established and provides a welcome message:
Create Session Route
A new tutoring session is created by sending a POST
request to /api/create_session
. The handler retrieves the user ID from the session using SessionManager
and passes it to the controller:
Send Query Route
Student queries are handled by a POST
request to /api/send_query
. The handler retrieves the user ID from the session using SessionManager
and passes it to the controller:
Session management in ASP.NET Core
is handled using middleware. Middleware are components that process HTTP requests and responses as they pass through the pipeline.
To enable session management, you need to:
-
Add session services in the builder:
-
Add the session middleware before defining your routes:
This setup allows you to store and retrieve session data using ISession
in your controller logic.
In this approach, the TutorController
is a plain C# class that is registered as a scoped service and used directly in the route handlers. It manages tutoring operations. Here is an example:
In this approach, instead of using model binding with [FromBody]
and data annotations, we parse the JSON request body manually using System.Text.Json
. This allows us to extract the required fields (session_id
and query
) from the incoming request and pass them to the service layer.
Each user is assigned a unique user_id
stored in the session. The SessionManager
class is responsible for ensuring a user_id
exists in the session and for retrieving it when needed. This ensures that each student's interactions are tracked and associated with their own session data, and avoids duplicating session logic across route handlers.
In this lesson, we successfully built a RESTful API for our personal tutor service using ASP.NET Core Web API
. We set up an ASP.NET Core
application, defined routes for tutoring operations, and explored how the TutorController
integrates with session management and structured responses. This lesson marks a significant milestone in our course, as it brings together all the components we've developed so far into a functional web application that can deliver personalized academic support at scale.
As you move on to the practice exercises, take the opportunity to experiment with the ASP.NET Core API
and reinforce the concepts covered in this lesson. This hands-on practice will prepare you for further development and exploration of additional ASP.NET Core
features and RESTful API concepts.
