ASP.NET Core Succinctly by Simone Chiaretta and Ugo Lattanzi Foreword by Daniel Jebaraj
Total Page:16
File Type:pdf, Size:1020Kb
1 ASP.NET Core Succinctly By Simone Chiaretta and Ugo Lattanzi Foreword by Daniel Jebaraj 2 Copyright © 2017 by Syncfusion, Inc. 2501 Aerial Center Parkway Suite 200 Morrisville, NC 27560 USA All rights reserved. Important licensing information. Please read. This book is available for free download from www.syncfusion.com on completion of a registration form. If you obtained this book from any other source, please register and download a free copy from www.syncfusion.com. This book is licensed for reading only if obtained from www.syncfusion.com. This book is licensed strictly for personal or educational use. Redistribution in any form is prohibited. The authors and copyright holders provide absolutely no warranty for any information provided. The authors and copyright holders shall not be liable for any claim, damages, or any other liability arising from, out of, or in connection with the information in this book. Please do not use this book if the listed terms are unacceptable. Use shall constitute acceptance of the terms listed. SYNCFUSION, SUCCINCTLY, DELIVER INNOVATION WITH EASE, ESSENTIAL, and .NET ESSENTIALS are the registered trademarks of Syncfusion, Inc. Technical Reviewer: James McCaffrey Copy Editor: Tres Watkins, content development manager, Syncfusion, Inc. Acquisitions Coordinator: Hillary Bowling, marketing coordinator, Syncfusion, Inc. Proofreader: Jacqueline Bieringer, content producer, Syncfusion, Inc. 3 Table of Contents The Story Behind the Succinctly Series of Books ................................................................. 8 About the Authors ..................................................................................................................10 Ugo Lattanzi ......................................................................................................................... 10 Simone Chiaretta .................................................................................................................. 10 About ASP.NET Core Succinctly ...........................................................................................11 Introduction to ASP.NET Core ...............................................................................................12 Chapter 1 What are .NET Core and ASP.NET Core? ............................................................13 .NET Core ............................................................................................................................ 13 ASP.NET Core ..................................................................................................................... 13 Chapter 2 A Brief History of the Microsoft Web Stack ........................................................14 ASP.NET Web Forms ........................................................................................................... 14 ASP.NET MVC ..................................................................................................................... 15 ASP.NET Web API ............................................................................................................... 15 OWIN and Katana ................................................................................................................ 15 What it brought to .NET Core ............................................................................................... 16 Chapter 3 Getting Started with .NET Core ............................................................................17 Installing .NET Core on Windows ......................................................................................... 17 Installing .NET Core on a Mac (or Linux) .............................................................................. 17 Building your first .NET Core application .............................................................................. 18 Command-line tools ............................................................................................................. 18 Visual Studio ........................................................................................................................ 21 Conclusion ........................................................................................................................... 22 Chapter 4 ASP.NET Core Basics ...........................................................................................23 Web app startup ................................................................................................................... 23 4 Program.cs ......................................................................................................................24 Startup.cs ........................................................................................................................25 Dependency injection ........................................................................................................... 26 What is dependency injection? ........................................................................................26 Configuring dependency injection in ASP.NET Core ........................................................27 Using dependency injection .............................................................................................28 Environments ....................................................................................................................... 31 Old approach ...................................................................................................................32 New approach .................................................................................................................32 Visual Studio ....................................................................................................................32 Startup class ....................................................................................................................37 Create your own environment ..........................................................................................37 Static files ............................................................................................................................. 39 Configure static files ........................................................................................................39 Error handling and exception pages ..................................................................................... 43 Developer exception page ...............................................................................................45 User-friendly error page ...................................................................................................46 Configuration files ................................................................................................................. 47 JSON format ....................................................................................................................48 Manage different environments ........................................................................................50 Dependency injection ......................................................................................................52 Logging ................................................................................................................................ 52 Configure logging .............................................................................................................54 Testing logging ................................................................................................................57 Change log verbosity .......................................................................................................58 Add a log to your application ............................................................................................59 5 Create your custom logger ...............................................................................................60 Conclusion ........................................................................................................................... 60 Chapter 5 Beyond the Basics: Application Frameworks .....................................................61 Web API ............................................................................................................................... 61 Installation .......................................................................................................................61 Playing around with URLs and verbs ...............................................................................62 Return data from an API ..................................................................................................64 Update data using APIs ...................................................................................................65 Testing APIs ....................................................................................................................67 ASP.NET MVC Core ............................................................................................................ 70 Routing ............................................................................................................................78 View specific features ........................................................................................................... 80 Tag Helpers .....................................................................................................................80 Building custom tag helpers .............................................................................................83