Lesson 3
Extending Functionality of Your ASP.NET Core Application
Introduction

Welcome to this lesson on extending the functionality of your ASP.NET Core application! By now, you should be familiar with the basics of ASP.NET Core and have successfully created a simple application that outputs "Hello, World!". In this lesson, we’ll elevate that basic application by understanding its structure, adding services, defining middleware, and creating endpoints. Ultimately, we aim to gain a better grasp of how to expand your ASP.NET Core application.

Simplest Program.cs file structure recap

In the previous lesson, we saw a very simple structure of the Program.cs file that consisted of just four lines of code:

C#
1var builder = WebApplication.CreateBuilder(args); 2var app = builder.Build(); 3 4app.MapGet("/", () => "Welcome to CodeSignal Learn!"); 5 6app.Run();

In this lesson, we'll explore how we can expand this structure and add more functionality to our application.

Extending the application functionality

You can extend the functionality of your application using several approaches:

  1. Registering Services: Services in ASP.NET Core are modular components that provide specific functionalities to the application. These services are essentially C# classes that are registered using the Services property of the WebApplicationBuilder. For example, you might add the HttpLoggingService, a built-in ASP.NET Core service that logs incoming requests. Once registered, the application becomes "aware" of the service and can instantiate and utilize it as needed. It's important to note that services must be registered before they can be used in middleware.

  2. Adding Middleware: Middleware is a series of components through which each HTTP request and response flows. For example, you can add HttpLogging middleware to log every incoming request. However, since this middleware depends on the HttpLoggingService, you need to register the service first. Middleware components execute in the order they are added.

  3. Mapping Endpoints: You can extend functionality by defining new endpoints. For instance, you might add an endpoint for a Todo app, enabling users to retrieve all Todo items.

Here's a schematic representation of how an extended Program.cs file can look:

C#
1var builder = WebApplication.CreateBuilder(args); 2 3// 1. Register services on the builder instance here 4 5var app = builder.Build(); 6 7// 2. Add middleware on the app instance here 8 9// 3. Map endpoints on the app instance here 10 11app.Run();

Don't worry if this doesn't make sense right away—we'll walk you through each of these approaches in the following sections.

Adding and configuring services

Services in ASP.NET Core are modular components that provide functionality to the application. These services can be framework-provided or custom, and they are registered using the Services property of the WebApplicationBuilder. Adding a service makes it available to the application. Some services may change the application's behavior immediately upon being registered, while others require you to specifically use them in the application.

Here’s how to add the HttpLogging service:

C#
1var builder = WebApplication.CreateBuilder(args); 2 3builder.Services.AddHttpLogging(opts => 4 opts.LoggingFields = HttpLoggingFields.RequestProperties); 5 6var app = builder.Build(); 7 8// ...

Services are essentially just classes that contain some functionality. Examples include logging services, authentication services, and custom services you create to meet specific needs.

Adding Middleware

Middleware components in ASP.NET Core form a pipeline through which each HTTP request and response flows. These components can handle requests, modify responses, and perform various other functions such as logging, authentication, and error handling. Middleware is added to the application in the order it should be executed, and each component can either pass the request to the next middleware or short-circuit the pipeline to handle the request itself.

Here's how to add the HttpLogging middleware after building the application:

C#
1var builder = WebApplication.CreateBuilder(args); 2 3// Services can be registered here 4 5var app = builder.Build(); 6 7app.UseHttpLogging(); // Adding HttpLogging middleware 8 9// Other middleware and endpoints go here 10 11app.Run();

The above code snippet shows how HttpLogging middleware is added to the request pipeline to log HTTP requests and responses. Each middleware component generally executes in the order it is added, so the sequence is significant. We'll cover middleware in more detail in the next lesson, so don't worry if you don't fully understand the concept yet.

Defining Endpoints

Endpoints in ASP.NET Core define how the application will handle HTTP requests at specific URLs. These endpoints can be used to handle various types of requests such as GET, POST, PUT, DELETE, etc. By mapping endpoints, you can create routes for different functionalities in your application, providing specific responses for each route.

Here's how to create and map some endpoints:

C#
1var builder = WebApplication.CreateBuilder(args); 2 3// Services can be registered here 4 5var app = builder.Build(); 6 7// Middleware can be registered here 8 9app.MapGet("/todo", () => 10{ 11 var todoItems = new[] 12 { 13 new { Id = 1, Title = "Learn ASP.NET Core", Completed = false }, 14 new { Id = 2, Title = "Build a web app", Completed = false }, 15 new { Id = 3, Title = "Deploy to cloud", Completed = false } 16 }; 17 return todoItems; 18}); 19 20app.Run();
Summary

In this lesson, we covered how to extend the functionality of your ASP.NET Core application by understanding the structure of the Program.cs file, adding and configuring services, and defining middleware and endpoints. You now have a basic understanding of how to expand and enhance the capabilities of your ASP.NET Core application. It's time to put your new knowledge into action and practice what we've just learned!

Enjoy this lesson? Now it's time to practice with Cosmo!
Practice is how you turn knowledge into actual skills.