6 Mapping URLs to endpoints using routing

 

This chapter covers

  • Mapping URLs to endpoint handlers
  • Using constraints and default values to match URLs
  • Generating URLs from route parameters

In chapter 5 you learned how to define minimal APIs, how to return responses, and how to work with filters and route groups. One crucial aspect of minimal APIs that we touched on only lightly is how ASP.NET Core selects a specific endpoint from all the handlers defined, based on the incoming request URL. This process, called routing, is the focus of this chapter.

This chapter begins by identifying the need for routing and why it’s useful. You’ll learn about the endpoint routing system introduced in ASP.NET Core 3.0 and why it was introduced, and explore the flexibility routing can bring to the URLs you expose.

The bulk of this chapter focuses on the route template syntax and how it can be used with minimal APIs. You’ll learn about features such as optional parameters, default parameters, and constraints, as well as how to extract values from the URL automatically. Although we’re focusing on minimal APIs in this chapter, the same routing system is used with Razor Pages and Model-View-Controller (MVC), as you’ll see in chapter 14.

6.1 What is routing?

6.2 Endpoint routing in ASP.NET Core

6.3 Exploring the route template syntax

6.3.1 Working with parameters and literal segments

6.3.2 Using optional and default values

6.3.3 Adding additional constraints to route parameters

6.3.4 Matching arbitrary URLs with the catch-all parameter

6.4 Generating URLs from route parameters

6.4.1 Generating URLs for a minimal API endpoint with LinkGenerator

6.4.2 Generating URLs with IResults

6.4.3 Controlling your generated URLs with RouteOptions

Summary

sitemap