Michele Aponte
Building Single Page Applications in .NET Core 3
Jumpstart Coding Using Blazor and C#
Michele Aponte
Torre del Greco (NA), Italy
Any source code or other supplementary material referenced by the author in this book is available to readers on GitHub via the books product page, located at www.apress.com/9781484257463 . For more detailed information, please visit www.apress.com/source-code .
ISBN 978-1-4842-5746-3 e-ISBN 978-1-4842-5747-0
https://doi.org/10.1007/978-1-4842-5747-0
Michele Aponte 2020
This work is subject to copyright. All rights are reserved by the Publisher, whether the whole or part of the material is concerned, specifically the rights of translation, reprinting, reuse of illustrations, recitation, broadcasting, reproduction on microfilms or in any other physical way, and transmission or information storage and retrieval, electronic adaptation, computer software, or by similar or dissimilar methodology now known or hereafter developed.
The use of general descriptive names, registered names, trademarks, service marks, etc. in this publication does not imply, even in the absence of a specific statement, that such names are exempt from the relevant protective laws and regulations and therefore free for general use.
The publisher, the authors and the editors are safe to assume that the advice and information in this book are believed to be true and accurate at the date of publication. Neither the publisher nor the authors or the editors give a warranty, expressed or implied, with respect to the material contained herein or for any errors or omissions that may have been made. The publisher remains neutral with regard to jurisdictional claims in published maps and institutional affiliations.
Distributed to the book trade worldwide by Springer Science+Business Media New York, 233 Spring Street, 6th Floor, New York, NY 10013. Phone 1-800-SPRINGER, fax (201) 348-4505, e-mail orders-ny@springer-sbm.com, or visit www.springeronline.com. Apress Media, LLC is a California LLC, and the sole member (owner) is Springer Science + Business Media Finance Inc (SSBM Finance Inc). SSBM Finance Inc is a Delaware corporation.
To my son, Francesco Paolo: I hope that one day you will read this book and be as proud of me as I am of you, especially every time you smile when you catch my eye.
Introduction
Blazor has garnered a great deal of enthusiasm since its initial release. Ive followed the framework from the beginning, and when teaching any new technology, I use a practical approach. This book looks at the needs of real applications and answers all the questions you might have when learning how to use Blazor.
Specifically, in Chapter , I focus on the success of this framework and how it solves one of the problems most felt by Microsoft programmers: using JavaScript. Blazor allows you to use .NET directly in the browser, using open standards so as not to repeat the mistakes of the past. In my opinion, however, it is important to understand how Blazor works beyond the tools made available by the development environment, so I focus on the code in this book. I start with a simple example, without using the templates made available by Microsoft, to highlight how it works.
In Chapter , you will find a detailed comparison between the two versions of Blazor for web development: Blazor Server and Blazor WebAssembly. Using the example from the first chapter, I compare the two versions by highlighting what is going on behind the scenes and how WebAssembly is revolutionizing the world of front-end development. I explain how to choose a version based your requirements because they both have pros and cons and must be contextualized in your environment/situation.
In Chapter , I cover all the concepts necessary to create a single-page application, starting from scratch and tackling some issues concerning the decomposition of the interface into components. In this chapter, you will learn how to create pages and navigate between them, you will see how to create forms for data entry, and you will learn how to integrate the front end with the back-end, differentiating the approach between the two versions of the framework but standardizing their use thanks to the dependency injection of ASP.NET Core. Here you will discover that the framework allows you to invoke JavaScript functions from .NET and to invoke .NET methods from JavaScript functions.
In Chapter , I explain how to create libraries of reusable components, addressing how to generalize components thanks to the frameworks ability to use .NET Generics and content projection. What may seem like more advanced aspects are actually necessary to avoid reinventing the wheel on each page of your application; this also allows you to start creating your own Blazor component library.
In Chapter , I cover application deployment, using both versions of the framework. Its important to know where your application will run, since scalability problems must be addressed and will impact how you write the application.
You can find the code for the first two chapters of the book in the countdown and countdown-wasm folders of the code download; you can use the two versions to see the differences between Blazor Server and Blazor WebAssembly within the same small application. The code for the third and fourth chapters, where you learn to create a small article manager, is available for both versions of the framework in eight pieces that follow the flow of the chapters.
The application structure
Pages and routing
Components and their use in pages
Back-end integration and shared library
JavaScript interoperability
Separation of the code into different files
Blazor Library
Custom input component
You can copy and execute the code that accompanies the book, but I suggest you write it from scratch by following the instructions in the chapters. Thats the best way to learn Blazor!
Acknowledgments
Writing a book is hard work that requires a lot of time and inevitably involves the lives of those closest to the author. I want to thank my life partner, Raffaella, for her support for this project and her patience with me. I also want to thank my old friend, whose name is Raffaella as well, for the help she gave me in revising my English.
A special thanks also goes to all the employees of Blexin, my company, who are the best team one could have and with whom I can experiment every day with the technologies described in this book.
Finally, many thanks to the fantastic Apress team that supported me during this project.
Table of Contents
About the Author
Michele Aponte
is a programmer who has worked with Java, .NET, and JavaScript at software and IT consulting companies in his native Italy since 1993. Combining his training, consulting, and development skills, in 2013 Michele founded Blexin to help customers migrate older software and systems to new technologies to improve their businesses. Passionate about programming, Michele embraces sharing with the community. He founded DotNetCampania, a Microsoft User Group in 2008, and has organized many regional conferences. He is also the founder of Blazor Developer Italiani, the Italian developer group about the Blazor framework. Recognized as a Microsoft MVP, he often presents on Microsoft and JavaScript topics at tech conferences throughout Italy.