Skip to main content
main-content
Top

About this book

Now in its 7th edition, the best selling book on MVC is updated for ASP.NET Core MVC 2. It contains detailed explanations of the Core MVC functionality which enables developers to produce leaner, cloud optimized and mobile-ready applications for the .NET platform. This book puts ASP.NET Core MVC into context and dives deep into the tools and techniques required to build modern, cloud optimized extensible web applications. All the new MVC features are described in detail and the author explains how best to apply them to both new and existing projects.

The ASP.NET Core MVC Framework is the latest evolution of Microsoft’s ASP.NET web platform, built on a completely new foundation. It represents a fundamental change to how Microsoft constructs and deploys web frameworks and is free of the legacy of earlier technologies such as Web Forms. ASP.NET Core MVC provides a "host agnostic" framework and a high-productivity programming model that promotes cleaner code architecture, test-driven development, and powerful extensibility.

Best-selling author Adam Freeman has thoroughly revised this market-leading book and explains how to get the most from ASP.NET Core MVC. He starts with the nuts-and-bolts and shows you everything through to advanced features, going in-depth to give you the knowledge you need. The book includes a fully worked case study of a functioning web application that readers can use as a template for their own projects.

What's New in This Edition

Fully updated for Visual Studio 2017, C# 7 and .NET Core 2

Coverage of new features such as view filters

Wider platform and tooling coverage than ever before, with more on Visual Studio Code and working with .NET Core on non-Windows platforms

Docker-based application deployment

What You Will Learn

Gain a solid architectural understanding of ASP.NET Core MVC

Explore the entire ASP.NET MVC Framework as a cohesive whole

See how MVC and test-driven development work in action

Learn what's new in ASP.NET Core MVC 2 and how best to apply these new features to your own work

See how to create RESTful web services and Single Page Applications

Build on your existing knowledge of previous MVC releases to get up and running with the new programming model quickly and effectively

Who This Book Is For

This book is for web developers with a basic knowledge of ASP.NET and C# who want to incorporate the latest improvements and functionality in the ASP.NET Core MVC 2 Framework.

Table of Contents

Introducing ASP.NET Core MVC 2

Frontmatter

Chapter 1. ASP .NET Core MVC in Context

Abstract
ASP.NET Core MVC is a web application development framework from Microsoft that combines the effectiveness and tidiness of model-view-controller (MVC) architecture, ideas and techniques from agile development, and the best parts of the .NET platform. In this chapter, you’ll learn why Microsoft created ASP.NET Core MVC, see how it compares to its predecessors and alternatives, and, finally, get an overview of what’s new in ASP.NET Core MVC and what’s covered in this book.
Adam Freeman

Chapter 2. Your First MVC Application

Abstract
The best way to appreciate a software development framework is to jump right in and use it. In this chapter, you’ll create a simple data-entry application using ASP.NET Core MVC. I take things one step at a time so you can see how an MVC application is constructed. To keep things simple, I skip over some of the technical details for the moment. But don’t worry. If you are new to MVC, you will find plenty to keep you interested. Where I use something without explaining it, I provide a reference to the chapter in which you can find all the details.
Adam Freeman

Chapter 3. The MVC Pattern, Projects, and Conventions

Abstract
Before digging into the details of ASP.NET Core MVC, I want to make sure you are familiar with the MVC design pattern, the thinking behind it, and the way it is translated into ASP.NET Core MVC projects. You might already know about some of the ideas and conventions I discuss in this chapter, especially if you have done advanced ASP.NET or C# development. If not, I encourage you to read carefully—a good understanding of what lies behind MVC can help put the features of the framework into context as you continue through the book.
Adam Freeman

Chapter 4. Essential C# Features

Abstract
In this chapter, I describe C# features used in web application development that are not widely understood or that often cause confusion. This is not a book about C#, however, and so I provide only a brief example for each feature so that you can follow the examples in the rest of the book and take advantage of these features in your own projects.
Adam Freeman

Chapter 5. Working with Razor

Abstract
In an ASP.NET Core MVC application, a component called the view engine is used to produce the content sent to clients. The default view engine is called Razor, and it processes annotated HTML files for instructions that insert dynamic content into the output sent to the browser.
Adam Freeman

Chapter 6. Working with Visual Studio

Abstract
In this chapter, I describe the key features that Visual Studio provides for developing ASP.NET Core MVC projects.
Adam Freeman

Chapter 7. Unit Testing MVC Applications

Abstract
In this chapter, I demonstrate how to unit test MVC applications. Unit testing is a form of testing in which individual components are isolated from the rest of the application so their behavior can be thoroughly validated.
Adam Freeman

Chapter 8. SportsStore: A Real Application

Abstract
ASP.NET Core MVC is a web application development framework from Microsoft that combines the effectiveness and tidiness of model-view-controller (MVC) architecture, ideas and techniques from agile development, and the best parts of the .NET platform. In this chapter, you’ll learn why Microsoft created ASP.NET Core MVC, see how it compares to its predecessors and alternatives, and, finally, get an overview of what’s new in ASP.NET Core MVC and what’s covered in this book.
Adam Freeman

Chapter 9. SportsStore: Navigation

Abstract
In this chapter, I continue to build out the SportsStore example app. In this chapter, I add support for navigating around the application and start building a shopping cart.
Adam Freeman

Chapter 10. SportsStore: Completing the Cart

Abstract
In this chapter, I continue to build the SportsStore example app. In the previous chapter, I added the basic support for a shopping cart, and now I am going to improve on and complete that functionality.
Adam Freeman

Chapter 11. SportsStore: Administration

Abstract
In this chapter, I continue to build the SportsStore application to give the site administrator a way of managing orders and products.
Adam Freeman

Chapter 12. SportsStore: Security and Deployment

Abstract
In the previous chapter, I added support for administering the SportsStore application, and it probably did not escape your attention that anyone could modify the product catalog if I deploy the application as it is. All they would need to know is that the administration features are available using the /Admin/Index and /Order/List URLs. In this chapter, I am going to show you how to prevent random people from using the administration functions by password-protecting them. Once I have the security in place, I will show you how to prepare and deploy the SportsStore application into production.
Adam Freeman

Chapter 13. Working with Visual Studio Code

Abstract
In this chapter, I show you how to create an ASP.NET Core MVC application using Visual Studio Code, which is an open source, cross-platform editor produced by Microsoft. Despite the name, Visual Studio Code is unrelated to Visual Studio and is based on the Electron framework, which is used by the Atom editor popular with developers of other web application frameworks such as Angular.
Adam Freeman

ASP.NET Core MVC 2 in Detail

Frontmatter

Chapter 14. Configuring Applications

Abstract
The topic of configuration may not seem interesting, but it reveals a lot about how MVC applications work and how HTTP requests are handled. Resist the temptation to skip this chapter, and take the time to understand the way that the configuration system shapes MVC web applications. It will give you a solid foundation for understanding the chapters that follow.
Adam Freeman

Chapter 15. URL Routing

Abstract
Early versions of ASP.NET assumed that there was a direct relationship between requested URLs and the files on the server hard disk. The job of the server was to receive the request from the browser and deliver the output from the corresponding file. This approach worked just fine for Web Forms, where each ASPX page is both a file and a self-contained response to a request.
Adam Freeman

Chapter 16. Advanced Routing Features

Abstract
In the previous chapter, I showed you how to use the routing system to handle incoming URLs, but this is only part of the story. You also need to be able use your URL schema to generate outgoing URLs you can embed in your views so that users can click links and submit forms back to your application in a way that will target the correct controller and action.
Adam Freeman

Chapter 17. Controllers and Actions

Abstract
This means controllers are responsible for processing incoming requests, performing operations on the domain model, and selecting views to render to the user.
Adam Freeman

Chapter 18. Dependency Injection

Abstract
Dependency injection can be a difficult topic to understand, both in terms of why it can be useful and how it is performed. To that end, I build up slowly, starting with the conventional way of building application components and gradually explaining how dependency injection works and why it matters.
Adam Freeman

Chapter 19. Filters

Abstract
Classic examples of crosscutting concerns are logging, authorization, and caching. In this chapter, I show you the different categories of filters that MVC supports, how to create and use custom filters, and how to control their execution.
Adam Freeman

Chapter 20. API Controllers

Abstract
Not all controllers are used to send HTML documents to clients. There are also API controllers, which are used to provide access to an application’s data. This is a feature that was previously provided through the separate Web API framework but has now been integrated into ASP.NET Core MVC. In this chapter, I explain the role that API controllers play in a web application, describe the problems they solve, and demonstrate how they are created, tested, and used.
Adam Freeman

Chapter 21. Views

Abstract
Throughout this book, you have seen views being used in many examples already, so you know roughly what they do, but I dig into the details in this chapter.
Adam Freeman

Chapter 22. View Components

Abstract
View components are classes that provide action-style logic to support partial views, which means complex content can be embedded in views while allowing the C# code that supports it to be easily maintained and unit tested.
Adam Freeman

Chapter 23. Understanding Tag Helpers

Abstract
Common uses for tag helpers include generating URLs for forms using the application’s routing configuration, ensuring that elements of a specific type are styled consistently, and replacing custom shorthand elements with commonly used fragments of content. In this chapter, I describe how tag helpers work and how custom tag helpers are created and applied.
Adam Freeman

Chapter 24. Using the Form Tag Helpers

Abstract
The form tag helpers are used to transform HTML form elements so that you don’t have to write custom tag helpers to solve the most common problems.
Adam Freeman

Chapter 25. Using the Other Built-in Tag Helpers

Abstract
In this chapter, I describe tag helpers that manage JavaScript and CSS stylesheets, create URLs for anchor elements, provide cache busting for image elements, and support data caching. I also describe the tag helper that provides support for application-relative URLs, which help ensure that browsers can access static content when an application is deployed into an environment shared with other applications.
Adam Freeman

Chapter 26. Model Binding

Abstract
In this chapter, I describe the way the model binding system works; show how it binds simple types, complex types, and collections; and demonstrate how you can take control of the process to specify which part of the request provides the data values your action methods require.
Adam Freeman

Chapter 27. Model Validation

Abstract
Throughout that chapter, I worked on the basis that the data the user supplied was valid. The reality is that users will often enter data that isn’t valid and cannot be used, which leads me to the topic of this chapter: model validation.
Adam Freeman

Chapter 28. Getting Started with Identity

Abstract
In this chapter, I demonstrate the process of setting up ASP.NET Core Identity and creating a simple user administration tool that manages individual user accounts that are stored in a database.
Adam Freeman

Chapter 29. Applying ASP.NET Core Identity

Abstract
In this chapter, I show you how to apply ASP.NET Core Identity to authenticate and authorize the user accounts created in the previous chapter.
Adam Freeman

Chapter 30. Advanced ASP.NET Core Identity

Abstract
In this chapter, I finish my description of ASP.NET Core Identity by showing you some of the advanced features it offers. I demonstrate how you can extend the database schema by defining custom properties on the user class and how to use database migrations to apply those properties without deleting the data in the ASP.NET Core Identity database. I also explain how ASP.NET Core Identity supports the concept of claims and demonstrate how they can be used to flexibly authorize access to action methods through policies. I finish the chapter by showing you how ASP.NET Core Identity makes it easy to authenticate users through third parties.
Adam Freeman

Chapter 31. Model Conventions and Action Constraints

Abstract
Throughout this book, I have emphasized that there is no magic involved in MVC development and that a small peek behind the scenes reveals how everything fits together to deliver the features that I have described in previous chapters.
Adam Freeman
Additional information