Chapter 5. Mapping URLs to methods using conventional routing

 

This chapter covers

  • Mapping URLs to action methods using conventions
  • Using constraints and default values to match URLs
  • Generating URLs from route parameters

In chapter 4, you learned about the MVC design pattern, and how ASP.NET Core uses it to generate the UI for an application. Whether you’re building a traditional HTML web application or creating a Web API for a mobile application, you can use the MvcMiddleware to generate a response. This is typically placed at the end of the middleware pipeline and handles requests after all the other middleware in the pipeline have executed.

In ASP.NET Core, you build MVC applications by creating controller classes that contain action methods. An action method executes in response to an appropriate request. It’s responsible for invoking the required business logic in the application model and determining what type of result to return. That might be a ViewResult indicating the template to use for HTML generation, a RedirectResult to forward the user to another URL, a StatusCodeResult if you’re writing a Web API, and so on.

Although not explicitly part of the classic MVC design pattern, one crucial part of the MVC pattern in ASP.NET Core is selecting the correct action method to invoke in response to a given request, as shown in figure 5.1. This process is called routing and is the focus of this chapter.

5.1. What is routing?

5.2. Routing to MVC controllers and actions

5.3. Routing using conventions

5.4. Handling multiple matching actions for a route

5.5. Generating URLs from route parameters

Summary

sitemap