Serving Your Personal Tutor with a RESTful API Using ASP.NET Core Web API

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.

Understanding RESTful APIs and ASP.NET Core Web API

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.

Initializing the ASP.NET Core Web API Project

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:

Building the App and Defining Routes

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:

Adding Session Management with ASP.NET Core Middleware

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:

  1. Add session services in the builder:

  2. Add the session middleware before defining your routes:

This setup allows you to store and retrieve session data using ISession in your controller logic.

Creating the TutorController

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:

Defining Request Models and Handling JSON

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.

Managing Student Sessions

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.

Summary and Next Steps

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.

Sign up
Join the 1M+ learners on CodeSignal
Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal