As the name suggests, MVC has three components: models, views, and controllers. Controllers handle incoming requests from a client or web browser and make decisions about what code to run. Views are templates (usually HTML plus some templating language like Handlebars, Pug, or Razor) that get data added to them and then are displayed to the user. Models hold the data that is added to views, or data that is entered by the user.
A common pattern for MVC code is:
- The controller receives a request and looks up some information in a database
- The controller creates a model with the information and attaches it to a view
- The view is rendered and displayed in the user's browser
- The user clicks a button or submits a form, which sends a new request to the controller
If you've worked with MVC in other languages, you'll feel right at home in ASP.NET Core MVC. If you're new to MVC, this chapter will teach you the basics and will help get you started.
The "Hello World" exercise of MVC is building a to-do list application. It's a great project since it's small and simple in scope, but it touches each part of MVC and covers many of the concepts you'd use in a larger application.
If you haven't already created a new ASP.NET Core project using
dotnet new mvc, follow the steps in the previous chapter. You should be able to build and run the project and see the default welcome screen.