ASP.NET Core 2 and Angular 5
Total Page:16
File Type:pdf, Size:1020Kb
www.EBooksWorld.ir ASP.NET Core 2 and Angular 5 Full-stack web development with .NET Core and Angular Valerio De Sanctis BIRMINGHAM - MUMBAI ASP.NET Core 2 and Angular 5 Copyright © 2017 Packt Publishing All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews. Every effort has been made in the preparation of this book to ensure the accuracy of the information presented. However, the information contained in this book is sold without warranty, either express or implied. Neither the author, nor Packt Publishing, and its dealers and distributors will be held liable for any damages caused or alleged to be caused directly or indirectly by this book. Packt Publishing has endeavored to provide trademark information about all of the companies and products mentioned in this book by the appropriate use of capitals. However, Packt Publishing cannot guarantee the accuracy of this information. First published: November 2017 Production reference: 1221117 Published by Packt Publishing Ltd. Livery Place 35 Livery Street Birmingham B3 2PB, UK. ISBN 978-1-78829-360-0 www.packtpub.com Credits Author Copy Editor Valerio De Sanctis Shaila Kusanale Reviewers Project Coordinator Ramchandra Vellanki Devanshi Doshi Juergen Gutsch Commissioning Editor Proofreader Ashwin Nair Safis Editing Acquisition Editor Indexer Reshma Raman Rekha Nair Content Development Editor Graphics Onkar Wani Jason Monteiro Technical Editor Production Coordinator Akhil Nair Aparna Bhagat About the Author Valerio De Sanctis is a skilled IT professional with over 12 years of experience in lead programming, web-based development, and project management using ASP.NET, PHP, and Java. He previously held senior positions at a range of financial and insurance companies, most recently serving as Chief Technology Officer and Chief Operating Officer at a leading after-sales and IT service provider for many top-tier life and non-life insurance groups. In the course of his career, Valerio De Sanctis helped many private organizations to implement and maintain .NET-based solutions, working side-by-side with many IT industry experts and leading several frontend, backend, and UX development teams. He designed the architecture and actively oversaw the development of a wide number of corporate-level web application projects for high-profile clients, customers, and partners, including London Stock Exchange Group, Zurich Insurance Group, Allianz, Generali, Harmonie Mutuelle, AIG, QBE, Marsh & McLennan, Lloyd's, Honda Motor, FCA Group, Luxottica, ANSA, Saipem, ENI, Enel, Terna, Banzai Media, Virgilio.it, Repubblica.it, and Corriere.it. He is an active member of the Stack Exchange Network, providing advice and tips for .NET, JavaScript, HTML5, and other web-related topics on the StackOverflow, ServerFault, and SuperUser communities. Most of his projects and code samples are available under open source licenses on GitHub, BitBucket, NPM, CocoaPods, JQuery Plugin Registry, and WordPress Plugin Repository. Starting from 2015, he also runs an IT-oriented, web-focused blog at www.ryadel.com featuring news, reviews, code samples and guides to help developers and enthusiasts worldwide. Between 2016 and 2017, he wrote two books on web development: ASP.NET Core and Angular 2, and ASP.NET Core: Cloud-ready, Enterprise Web Application Development, with over 5,000 copies sold worldwide. I would like to thank those who supported me in writing this book: my beloved and beautiful wife, Carla, for her awesome encouragement and invaluable support; my children, Viola and Daniele; my parents and my sister for always being there in times of need; and my IT friends, colleagues, and partners working at Ryadel.com, Kapusons, Teleborsa and Assirecre Group for their enduring friendship. A special thanks to Onkar, Reshma, and all Packt Publishing folks who worked hard to bring this book to life. Last but not least, I would like to thank you, the reader, for picking up this book. I really hope you will enjoy it! About the Reviewers Ramchandra Vellanki is a passionate programmer. He has 13 years of programming experience, has worked in different roles, and has experience in building and maintaining large-scale products/applications. He started his career with IBM iSeries, and then worked on C++, MFC, .NET, and JavaScript. Currently, he is working on .NET and JavaScript technologies. He enjoys exploring and learning new technologies. I would like to thank my parents (Saroja and Ramaiah), wife (Sirisha) and kids (Abhi and Ani) for their love, understanding, and constant support. I also would like to thank all my friends and relatives for their continuous encouragement and support throughout my career and life. Juergen Gutsch is a .NET-addicted web developer. He has been working with .NET and ASP.NET since the early versions in 2002. Before that, he wrote server-side web applications using classic ASP. Juergen is also an active person in the German speaking .NET developer community. He is leading the .NET user group in Basel (Switzerland) and the INETA Germany Association. Due to that, Microsoft awarded him to be a Microsoft Most Valuable Professional in the Visual Studio and Development Technologies category several times. Jurgen writes for the dotnetpro magazine, one of the most popular German-speaking developer magazines, and he also publishes articles in English on his blog at https:/ / asp. net- hacker. rocks. The best way to contact him and to stay in touch with him is using Twitter--https:/ / twitter. com/ sharpcms. He is working as a developer, consultant, and trainer for the digital agency YooApplications Inc. (http:/ / yooapps. com), located in Basel, Switzerland. YooApplications serves national as well as international clients and specializes in creating custom digital solution for distinct business needs. www.PacktPub.com For support files and downloads related to your book, please visit www.PacktPub.com. Did you know that Packt offers eBook versions of every book published, with PDF and ePub files available? You can upgrade to the eBook version at www.PacktPub.com and as a print book customer, you are entitled to a discount on the eBook copy. Get in touch with us at [email protected] for more details. At www.PacktPub.com, you can also read a collection of free technical articles, sign up for a range of free newsletters and receive exclusive discounts and offers on Packt books and eBooks. https:/ / www. packtpub. com/ mapt Get the most in-demand software skills with Mapt. Mapt gives you full access to all Packt books and video courses, as well as industry-leading tools to help you plan your personal development and advance your career. Why subscribe? Fully searchable across every book published by Packt Copy and paste, print, and bookmark content On demand and accessible via a web browser Customer Feedback Thanks for purchasing this Packt book. At Packt, quality is at the heart of our editorial process. To help us improve, please leave us an honest review on this book's Amazon page at https://www.amazon.com/dp/1788293606. If you'd like to join our team of regular reviewers, you can e-mail us at [email protected]. We award our regular reviewers with free eBooks and videos in exchange for their valuable feedback. Help us be relentless in improving our products! Table of Contents Preface 1 Chapter 1: Getting Ready 9 Two players, one goal 10 The ASP.NET core revolution 10 What's new in Angular? 11 A full-stack approach 12 Single-Page Application 13 Common features of a competitive SPA 13 Product Owner expectations 15 A sample Single-Page Application project 17 The vision 17 Not your usual "Hello World!" 18 Introducing TestMakerFree 18 Core features and requirements 19 Preparing the workspace 21 Disclaimer-Do (not) try this at home 21 The broken code myth 21 Stay hungry, stay foolish, yet be responsible as well 23 Versions and builds 23 Setting up the project 24 Alternative setup using the command line 28 Test run 29 Looking around 30 The configuration files 32 Program.cs 32 Startup.cs 34 The appsettings.json file 38 The package.json file 39 Upgrading (or downgrading) Angular 40 Upgrading (or downgrading) the other packages 41 The tsconfig.json file 43 The webpack configuration files 45 Updating the webpack.config.js file 47 Patching the webpack.config.vendor.js file 48 Why use a dynamic module bundler? 49 Refreshing the Webpack-generated files 49 The server-side code 50 Controllers/HomeController.cs 50 Table of Contents Controllers/SampleDataController.cs 51 The /Views/ folder 52 The client-side code 53 The /ClientApp/app/ folder 54 Getting to work 56 Static file caching 56 A blast from the past 57 Back to the future 58 Testing it up 61 The strongly-typed approach(es) 62 Client app cleanup 63 Trimming down the component list 63 The AppModule class(es) 66 Updating the NavMenu 67 References 68 Suggested topics 69 Summary 69 Chapter 2: Backend with .NET Core 70 The data flow 71 The role of ViewModel 74 Our first ViewModel 74 QuizViewModel 75 QuizController 76 Adding more action methods 78 ByTitle() 79 Random() 79 Testing it up 80 Adding other controllers 81 QuestionViewModel 81 QuestionController 82 AnswerViewModel 84 AnswerController 85 ResultViewModel 86 ResultController 87 Understanding routes 89 Defining routing 89 Routing through the ages 89 Handling routes in .NET Core 91