Skip to main content

Über dieses Buch

Now in its 6th edition, the best selling book on MVC is now updated for ASP.NET Core MVC. It contains detailed explanations of the new 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.

This book follows the same format and style as the popular previous editions but brings everything up to date for the new ASP.NET Core MVC release. It presents a fully worked case study of a functioning ASP.NET MVC application that readers can use as a template for their own projects.

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 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 new ASP.NET Core MVC Framework.



Introducing ASP.NET Core MVC


Chapter 1. ASP.NET Core MVC in Context

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.


Chapter 2. Your First MVC Application

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 the ASP.NET Core MVC. I take things a 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

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.


Chapter 4. Essential C# Features

In this chapter, I describe C# features used in web application development that are not widely understood or 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

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

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

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. ASP.NET Core MVC has been designed to make it easy to create unit tests, and Visual Studio provides support for a wide range of unit testing frameworks. I show you how to set up a unit test project, explain how to install one of the most popular testing frameworks, and describe the process for writing and running tests.

Adam Freeman

Chapter 8. SportsStore: A Real Application

In the previous chapters, I built quick and simple MVC applications. I described the MVC pattern, the essential C# features, and the kinds of tools that good MVC developers require. Now it is time to put everything together and build a simple but realistic e-commerce application.

Adam Freeman

Chapter 9. SportsStore: Navigation

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

Adam Freeman

Chapter 10. SportsStore: Completing the Cart

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

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

Adam Freeman

Chapter 12. SportsStore: Security and Deployment

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

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 in Detail


Chapter 14. Configuring Applications

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 is worth your time and will give you a solid foundation for understanding the chapters that follow.

Adam Freeman

Chapter 15. URL Routing

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

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

Every request that comes to your application is handled by a controller. In the ASP.NET Core MVC, controllers are .NET classes that contain the logic required to handle a request. In Chapter 3, I explained that the role of the controller is to encapsulate your application logic. This means that 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

In this chapter, I describe dependency injection (DI), a technique that helps create flexible applications and simplifies unit testing. 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. Table 18-1 puts dependency injection into context.

Adam Freeman

Chapter 19. Filters

Filters inject extra logic into MVC request processing. They provide a simple and elegant way to implement cross-cutting concerns—a term that refers to functionality that is used all over an application and doesn’t fit neatly into any one place, where it would break the separation of concerns. Classic examples of cross-cutting 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. Table 19-1 puts filters in context

Adam Freeman

Chapter 20. API Controllers

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. Table 20-1 puts API controllers in context.

Adam Freeman

Chapter 21. Views

In Chapter 17, you saw how action methods can return ViewResult objects, which tells MVC to render a view and return an HTML response to the client.

Adam Freeman

Chapter 22. View Components

View components are classes that provide action-style logic to support partial views, which means complex content to 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

Tag helpers are a new feature that has been introduced in ASP.NET Core MVC and are C# classes that transform HTML elements in a view. 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

MVC provides a set of built-in tag helpers that are used to perform commonly required transformations on HTML elements. In this chapter, I describe the tag helpers that operate on HTML forms, which include the form, input, label, select, option, and textarea elements. In Chapter 25, I describe the other built-in tag helpers, which provide nonform features. Table 24-1 puts the form tag helpers in context.


Chapter 25. Using the Other Built-in Tag Helpers

The tag helpers that I described in Chapter 24 are focused on producing HTML forms, but they are not the only built-in tag helpers that are provided by ASP.NET Core MVC. 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. Table 25-1 summarizes the chapter.


Chapter 26. Model Binding

Model binding is the process of creating .NET objects using the data from the HTTP request in order to provide action methods with the arguments they need. 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. Table 26-1 puts model binding in context.


Chapter 27. Model Validation

In the previous chapter, I showed you how MVC creates model objects from HTTP requests through the model binding process. 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.


Chapter 28. Getting Started with Identity

ASP.NET Core Identity is an API from Microsoft to manage users in ASP.NET applications. 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.


Chapter 29. Applying ASP.NET Core Identity

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. Table 29-1 summarizes this chapter.


Chapter 30. Advanced ASP.NET Core Identity

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. I demonstrate authentication with Google accounts, but ASP.NET Core Identity has built-in support for Microsoft, Facebook, and Twitter accounts as well. Table 30-1 summarizes this chapter.


Chapter 31. Model Conventions and Action Constraints

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.



Weitere Informationen

Premium Partner