Book Image

ASP.NET Core MVC 2.0 Cookbook

By : Jason De Oliveira, Engin Polat, Stephane Belkheraz
Book Image

ASP.NET Core MVC 2.0 Cookbook

By: Jason De Oliveira, Engin Polat, Stephane Belkheraz

Overview of this book

The ASP.NET Core 2.0 Framework has been designed to meet all the needs of today’s web developers. It provides better control, support for test-driven development, and cleaner code. Moreover, it’s lightweight and allows you to run apps on Windows, OSX and Linux, making it the most popular web framework with modern day developers. This book takes a unique approach to web development, using real-world examples to guide you through problems with ASP.NET Core 2.0 web applications. It covers Visual Studio 2017- and ASP.NET Core 2.0-specifc changes and provides general MVC development recipes. It explores setting up .NET Core, Visual Studio 2017, Node.js modules, and NuGet. Next, it shows you how to work with Inversion of Control data pattern and caching. We explore everyday ASP.NET Core MVC 2.0 patterns and go beyond it into troubleshooting. Finally, we lead you through migrating, hosting, and deploying your code. By the end of the book, you’ll not only have explored every aspect of ASP.NET Core MVC 2.0, you’ll also have a reference you can keep coming back to whenever you need to get the job done.
Table of Contents (26 chapters)
Title Page
Copyright and Credits
Packt Upsell
Contributors
Preface
Index

Preface

The ASP.Net Core 2.0 Framework is designed to meet the needs of today's web developers. It provides better control, support for test-driven development, and cleaner code. Moreover, it's lightweight and allows you to run apps on Windows, OSX, and Linux, making it the most popular web framework with modern day developers.

This book follows a unique approach to web development, using real-world examples that guide you through problems with ASP.NET Core 2.0 web applications. It covers VS2017 and ASP.NET Core 2.0-specific changes, and provides general MVC development recipes. It explores setting up .NET Core, Visual Studio 2017, Node.js modules, and NuGet. Next, it shows you how to work with Inversion of Control data pattern and caching. We explore everyday ASP.NET Core MVC  2.0 patterns and go beyond ASP.NET Core MVC 2.0 , into troubleshooting. Finally, we will lead you through migrating, hosting, and deploying your code.

By the end of this book, you'll not only have explored every aspect of ASP.NET Core 2.0, you'll also have a reference you can keep coming back to whenever you need to get the job done.

Who this book is for

This book is for developers who want to learn to build web applications and make a career building web applications using ASP.NET Core.

If you're new to programming, this book will introduce you to the patterns and concepts used to build modern web applications.

This book is also for those who are working on other web frameworks and want to learn more about ASP.NET Core.

You'll learn how to build a web app from scratch; this book will give you a starting point so you can learn more advanced topics.

What this book covers

Chapter 1, Cross-Platform with .NET Core, dives deep into what .NET Core is, what its core components are, and creating ASP.NET Core web projects using the .NET Core CLI.

Chapter 2,  Visual Studio 2017, C# 6, IDEs, and Roslyn, covers the use of Visual Studio 2017, Visual Studio Code, and Atom as C# editors. Also, we'll investigate the new features that come with C# 6 and use Roslyn compiler as a service.

Chapter 3, Working with NPM, Frontend Package Managers, and Task Runners, covers using and creating npm modules and using Bower, Grunt, Gulp, and Yeoman.

Chapter 4, Reusing Code with NuGet, guides you though manage NuGet packages, creating new NuGet packages, using previously created packages, and creating a new NuGet repository to privately list NuGet packages.

Chapter 5SOLID Principles, Inversion of Control, and Dependency Injection, covers all SOLID principles, plus the inversion of Ccontrol and dependency injection libraries that come with ASP.NET Core and seperate NuGet packages.

Chapter 6, Data Access - Entity Framework with Repository, SQL Server, and Stored Procedures, covers implementing the repository pattern using Entity Framework Core. During implementation, we'll cover the IoC life cycle. Also, we'll investigate different providers of Entity Framework Core.

Chapter 7, Data Access with Micro ORMs, NoSQL, and Azure, covers creating DataAccessLayer with several libraries, such as Dapper and OrmLite. Also, we'll investigate MongoDB, Azure table storage, Azure blob storage, and SQL on Azure.

Chapter 8, Cache and Session - Distributed, Server, and Client, covers caching with several providers, such as InMemory Data Caching, Response Caching, Session Caching, and Distributed Caching. We'll use Redis on both on-premises and Azure.

Chapter 9, Routing, discusses the different aspects of ASP.NET Routing mechanisms, Route Constraints, and SEO-friendly routing.

Chapter 10, ASP.NET Core MVC, covers ActionResult types, creating and using POCO controllers, and managing Exceptions.

Chapter 11, Web API, discusses managing content-negotiation, handling cross-domain requests, auto-documenting controllers and actions using Swagger, and testing actions.

Chapter 12, Filters, covers how to create, manage, and use Global Filters for injecting tasks before and after the execution of actions, such as logging, caching, controlling authentication, and authorization.

Chapter 13, Views, Models, and ViewModels, covers creating and using ViewModels, and understanding and using the ModelBinding mechanism and ValueProviders.

Chapter 14Razor and Views, talks about views, partial views, areas, HTML helpers, and dependency injection into views.

Chapter 15TagHelpers and ViewComponents, dives deep into using Environment, Script, Form, and Link TagHelpers. We'll also create reusable ViewComponents.

Chapter 16OWIN and Middlewares, describes OWIN and Katana, the new ASP.NET Core pipelines and middleware, along with creating and using ASP.NET Core middleware.

Chapter 17, Security, covers Authentication and Authorization mechanisms, managing Identity, and securing data with ASP.NET Core.

Chapter 18Frontend Development, covers how to use Bootstrap classes, write maintainable and clean JavaScript code, and debug it. We will see what TypeScript is and write unit tests for JavaScript code.

Chapter 19Deployment and Hosting, covers hosting ASP.NET Core web applications using IIS, Azure, and Docker. We'll also host ASP.NET web applications on Linux, Windows, and Mac environments using Kestrel.

To get the most out of this book

  1. It's beneficial to have some web backend or frontend programming experience.
  2. You'll need Visual Studio 2017, Visual Studio Code, Atom, SQL Server Management Studio, and a modern web browser installed. Don't worry, we'll guide you on how to install them when needed.
  3. You'll need an active Azure subscription. The Microsoft Azure platform offers free trial accounts at http://www.azure.com

Download the example code files

You can download the example code files for this book from your account at www.packtpub.com. If you purchased this book elsewhere, you can visit www.packtpub.com/support and register to have the files emailed directly to you.

You can download the code files by following these steps:

  1. Log in or register at www.packtpub.com.
  2. Select the SUPPORT tab.
  3. Click on Code Downloads & Errata.
  4. Enter the name of the book in the Search box and follow the onscreen instructions.

Once the file is downloaded, please make sure that you unzip or extract the folder using the latest version of:

  • WinRAR/7-Zip for Windows
  • Zipeg/iZip/UnRarX for Mac
  • 7-Zip/PeaZip for Linux

The code bundle for the book is also hosted on GitHub at https://github.com/PacktPublishing/ASP.NET-MVC-Core-2.0-Cookbook. We also have other code bundles from our rich catalog of books and videos available at https://github.com/PacktPublishing/. Check them out!

Download the color images

We also provide a PDF file that has color images of the screenshots/diagrams used in this book. You can download it here: https://www.packtpub.com/sites/default/files/downloads/ASP.NETMVCCore2.0Cookbook_ColorImages.pdf.

Conventions used

There are a number of text conventions used throughout this book.

CodeInText: Indicates code words in text, database table names, folder names, filenames, file extensions, pathnames, dummy URLs, user input, and Twitter handles. Here is an example: "Run dotnet new mvc from the command line. This command will create a new ASP.NET Core MVC project with the same name as the containing folder."

A block of code is set as follows:

public class Person
{
    public class Name {get;set;}
    public Person(){
        Name = "Stephane";
    }
}

When we wish to draw your attention to a particular part of a code block, the relevant lines or items are set in bold:

public class Person
{
public class Name {get;set;}
    public Person(){
        Name = "Stephane";
    }
}

Any command-line input or output is written as follows:

$ apt-get install nautilus-open-terminal 
 
$ killall nautilus && nautilus

Bold: Indicates a new term, an important word, or words that you see onscreen. For example, words in menus or dialog boxes appear in the text like this. Here is an example: "Select the Subscription."

Note

Warnings or important notes appear like this.

Note

Tips and tricks appear like this.

Sections

In this book, you will find several headings that appear frequently (Getting ready, How to do it..., How it works..., There's more..., and See also).

To give clear instructions on how to complete a recipe, use these sections as follows:

Getting ready

This section tells you what to expect in the recipe and describes how to set up any software or any preliminary settings required for the recipe.

How to do it…

This section contains the steps required to follow the recipe.

How it works…

This section usually consists of a detailed explanation of what happened in the previous section.

There's more…

This section consists of additional information about the recipe in order to make you more knowledgeable about the recipe.

See also

This section provides helpful links to other useful information for the recipe.

Get in touch

Feedback from our readers is always welcome.

General feedback: Email [email protected] and mention the book title in the subject of your message. If you have questions about any aspect of this book, please email us at [email protected].

Errata: Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you have found a mistake in this book, we would be grateful if you would report this to us. Please visit www.packtpub.com/submit-errata, selecting your book, clicking on the Errata Submission Form link, and entering the details.

Piracy: If you come across any illegal copies of our works in any form on the internet, we would be grateful if you would provide us with the location address or website name. Please contact us at [email protected] with a link to the material.

If you are interested in becoming an author: If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, please visit authors.packtpub.com.

Reviews

Please leave a review. Once you have read and used this book, why not leave a review on the site that you purchased it from? Potential readers can then see and use your unbiased opinion to make purchase decisions, we at Packt can understand what you think about our products, and our authors can see your feedback on their book. Thank you!

For more information about Packt, please visit packtpub.com.