19 Creating a website with MVC controllers

 

This chapter covers

  • Creating a Model-View-Controller (MVC) application
  • Choosing between Razor Pages and MVC controllers
  • Returning Razor views from MVC controllers

In this book I’ve focused on Razor Pages over MVC controllers for server-rendered HTML apps, as I consider Razor Pages to be the preferable paradigm in most cases. In this chapter we dig a bit more into exactly why I consider Razor Pages to be the right choice and take a brief look at the alternative.

In section 19.2 you’ll create a default MVC application using a template so you can familiarize yourself with the general project layout of an MVC application. We’ll look at some of the differences between an MVC application and a Razor Pages app, as well as the many similarities.

Next, I’ll dig into why I find Razor Pages to be a preferable application model compared with MVC controllers. You’ll learn about the improved developer ergonomics of Razor Pages compared with MVC controllers, as well as the cases in which MVC controllers are nevertheless the right choice.

In section 19.4 you’ll learn about rendering Razor views using MVC controllers. You’ll learn how the MVC framework relies on conventions to locate view files and how to override these by selecting a specific Razor view template to render. Finally, you’ll see the full view selection algorithm in all its glory.

19.1 Razor Pages vs. MVC in ASP.NET Core

19.2 Your first MVC web application

19.3 Comparing an MVC controller with a Razor Page PageModel

19.4 Selecting a view from an MVC controller

19.5 Choosing between Razor Pages and MVC controllers

19.5.1 The benefits of Razor Pages

19.5.2 When to choose MVC controllers over Razor Pages

Summary