23,99 €
ASP.NET Core is packed full of hidden features for building sophisticated web applications – but if you don’t know how to customize it, you’re not making the most of its capabilities. Customizing ASP.NET Core 6.0 is a book that will teach you all about tweaking the knobs at various layers and take experienced programmers’ skills to a new level.
This updated second edition covers the latest features and changes in the .NET 6 LTS version, along with new insights and customization techniques for important topics such as authentication and authorization. You’ll also learn how to work with caches and change the default behavior of ASP.NET Core apps.
This book will show you the essential concepts relating to tweaking the framework, such as configuration, dependency injection, routing, action filters, and more. As you progress, you'll be able to create custom solutions that meet the needs of your use case with ASP.NET Core. Later chapters will cover expert techniques and best practices for using the framework for your app development needs, from UI design to hosting. Finally, you'll focus on the new endpoint routing in ASP.NET Core to build custom endpoints and add third-party endpoints to your web apps for processing requests faster.
By the end of this book, you'll be able to customize ASP.NET Core to develop better, more robust apps.
Das E-Book können Sie in Legimi-Apps oder einer beliebigen App lesen, die das folgende Format unterstützen:
Seitenzahl: 151
Veröffentlichungsjahr: 2021
Learn to turn the right screws to optimize ASP.NET Core applications for better performance
Jürgen Gutsch
BIRMINGHAM—MUMBAI
Copyright © 2022 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 or its dealers and distributors, will be held liable for any damages caused or alleged to have been 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.
Associate Group Product Manager: Pavan Ramchandani
Senior Editor: Mark Dsouza
Content Development Editor: Divya Vijayan
Technical Editor: Saurabh Kadave
Copy Editor: Safis Editing
Project Coordinator: Rashika Ba
Proofreader: Safis Editing
Indexer: Sejal Dsilva
Production Designer: Prashant Ghare
Marketing Coordinator: Anamika Singh
First published: January 2021
Second edition: December 2021
Production reference: 2270122
Published by Packt Publishing Ltd.
Livery Place
35 Livery Street
Birmingham
B3 2PB, UK.
ISBN 978-1-80323-360-4
www.packt.com
To my family, friends, colleagues, and the .NET developer community, who supported and encouraged me to do this work. To my three sons, who gave me the power to complete it.
– Jürgen Gutsch
I have known Jürgen for more than 7 years and he never ceases to impress me with his knowledge and his experience in software development and web development, his expertise in ASP.NET Core and Azure solutions, and his consultancy and software architecture skills. Jürgen has been involved with ASP.NET since the first releases and has a vast amount of experience with all the .NET and .NET Core versions. His journey has now moved on to Azure and application development in the cloud using the latest ASP. NET Core technologies, Azure DevOps, and cloud-based solutions.
Jürgen is one of the leading community experts in the Swiss and German region and is extremely active in this field. You will always find him present at local conferences, .NET Core meetups, and Microsoft events, or organizing these events himself. He has been a Microsoft MVP for Developer Technologies since 2015 and I have had the pleasure to develop and learn from him by implementing solutions using some of the early versions and pre-releases of ASP.NET Core or solutions such as ASP.NET Core health checks. He is always helping the .NET community by giving them a chance to speak at meetups and offering speakers a platform. I did my first .NET user group talk in Basel with his help and support.
Above all, he is always there as a friend and I can always ask for support or his help in solving problems, and he helps make the .NET community in Switzerland so strong, helpful, and easy for others to join and improve.
Damien Bowden
Blogger on damienbod.com, Microsoft MVP for Developer Technologies, and Senior Software Consultant.
Well known on Twitter as @damien_bod
Personally, I believe we should always try to improve. I often ask myself three questions after I do anything:
How could I have done that faster? How could I have done that better? How do I make sure I don't have to do that again?By asking these questions, we can always improve in everything we do. That last question involves strategic scale, which is trickier (you usually have to improve with #1 and #2 to get to an answer for #3). For example, if you clean a coffee machine every night, you might ask yourself, "How can I clean that faster, how can I make it even cleaner, and how do I make sure I don't have to do that again?" You might not be able to answer that third question for quite a while, but pretty soon, you'd be an expert, in speed and quality, at cleaning coffee pots (possibly the best in the world). As you work to answer the third question, you'd start by training the people around you, which means more people would be cleaning (and you'd be doing it less). Finally, you'd likely get promoted to manager (since you're already training everyone). Thus, you would no longer have to clean coffee pots!
"Sounds good, Ed, but what does this have to do with code?" Great question, me! To answer that, let's head to Grace Hopper, one of our key leaders in pioneering computer technology…
"The most dangerous phrase in the language is, 'We've always done it this way.'"
– Grace Hopper
As Grace implies, our greatest danger is when we stop learning and ultimately stop improving our products and our code! As the CEO of Microsoft eloquently said:
"Always keep learning.You stop doing useful things if you don't learn."
– Satya Nadella
And if we don't learn and improve our code, then our product remains stagnant. But if we keep learning and improving, then not only does our product get faster (which answers question #1) and more useful (question #2), but it also makes us far more valuable to our product and company! You might want to keep coding for the rest of your life, but if you're providing that much value to your team, then there will be many opportunities to train them and potentially even manage them (which means coding less often, if that's appealing to you). At the very least, it will open many opportunities in your career!
In Customizing ASP.NET Core 6.0, Microsoft MVP Jürgen Gutsch brings his nearly 20 years of experience as a .NET and ASP.NET web developer to this book. Following Grace Hopper's and Satya Nadella's pleas for us to never stop learning and improving, Jürgen shows you how to do that to customize, improve, and optimize your ASP.NET Core applications. He shows you how to customize various aspects of your application, including the hosting layer (logging, app configuration, dependency injection, HTTPS configuration with Kestrel, and hosting models), the middleware layer, the routing layer (endpoint routing), the MVC layer (customizing and configuring identity management), and the Web API layer (content negotiation and managing inputs)! By the time you're done with this book, you'll have a richer understanding of .NET development and how you can get the most out of your applications.
You'll find that each chapter begins with a high-level architecture diagram that visually explains the chapter's architecture and which layer of the architecture the chapter is referring to. Use these diagrams to create a strong mental picture of how the architectural layers build on each other. Even if you understand the topic of customization in each chapter, you should try out the code to become more familiar with the various opportunities that you'll have to improve your application. Whether you're building a custom logger, customizing dependency injection, or enabling customers to manage their profiles, this book thoroughly goes through each step of app customization and optimization. As Satya Nadella implores, here's your chance to "always keep learning." Jump in and join Jürgen Gutsch on this learning journey.
Ed Price
Senior Program Manager of Architectural Publishing at Microsoft
Azure Architecture Center (http://aka.ms/Architecture) Co-author of six books, including The Azure Cloud Native Architecture Mapbook and ASP.NET Core 5 for Beginners (both from Packt)
Jürgen Gutsch is a .NET-addicted web developer. He has worked with .NET and ASP.NET since the early versions in 2002. Before that, he wrote server-side web applications using classic ASP. He is also an active part of the .NET developer community. Jürgen writes for the dotnetpro magazine, one of the most popular German-speaking developer magazines. He also publishes articles in English on his blog, ASP.NET Hacker, and contributes to several open source projects. Jürgen has been a Microsoft MVP since 2015.
The best way to contact him is by using Twitter: @sharpcms.
He works as a developer, consultant, and trainer for the digital agency YOO Inc., located in Basel, Switzerland. YOO Inc. serves national as well as international clients and specializes in creating custom digital solutions for distinct business needs.
I want to thank my boss, my colleagues, and my employer for their support and motivation.
Toi B. Wright has been obsessed with ASP.NET for almost 20 years. She is the founder and president of the Dallas ASP.NET User Group. She has been a Microsoft MVP in ASP.NET for 16 years and is also an ASPInsider. She is an experienced full-stack software developer, book author, courseware author, speaker, and community leader with over 25 years of experience. She has a B.S. in computer science and engineering from the Massachusetts Institute of Technology (MIT) and an MBA from Carnegie Mellon University (CMU).
You can find her on Twitter: @misstoi.
ASP.NET Core is the most powerful web framework provided by Microsoft and is full of hidden features that make it even more powerful and useful.
Your application should not be made to match the framework; your framework should be able to do what your application really needs. With this book, you will learn how to find the hidden screws you can turn to get the most out of the framework.
Developers working with ASP.NET Core will be able to put their knowledge to work with this practical guide to customizing ASP.NET Core. The book provides a hands-on approach to implementation and its associated methodologies that will have you up and running and productive in no time.
This book is a compact collection of default ASP.NET Core behaviors you might want to change and step-by-step explanations of how to do so.
By the end of this book, you will know how to customize ASP.NET Core to get an optimized application out of it according to your individual needs.
To follow the next chapters, you should be familiar with the base architecture of ASP.NET Core and its components. This book tackles almost all of the components of the architecture.
The following figure shows the base architecture overview of ASP.NET Core 6.0. Let's quickly go through the components shown here from the bottom to the top layer:
At the bottom, there is the Host layer. This is the layer that bootstraps the web server and all the stuff that is needed to start up an ASP.NET Core application, including logging, configuration, and the service provider. This layer creates the actual request objects and their dependencies that are used in the layers above.
The next layer above Host is the Middleware layer. This layer works with the request object or manipulates it. This attaches the middleware to the request object. It executes the middleware for things such as error handling, authenticating HSTS, CORS, and so on.
Above that, there is the Routing layer, which routes the request to the endpoints depending on the route patterns defined. Endpoint routing is the new player from ASP.NET Core 3.1 and separates routing from the UI layers above to enable routing for different endpoints, including Blazor, gRPC, and SignalR. As a reminder: in previous versions of ASP.NET Core, routing was part of the MVC layer, and every other UI layer needed to implement its own routing.
The actual endpoints are provided by the fourth layer, the UI layer, which contains the well-known UI frameworks Blazor, gRPC, SignalR, and MVC. This is where you will do most of your work as an ASP.NET Core developer.
Lastly, above MVC, you will find WebAPI and Razor Pages.
This book doesn't cover all the topics mentioned in the architecture overview. This book covers most topics of the host layer because that is the layer that contains the most things you might need to customize. This book tackles middleware and routing, as well as MVC features and some more WebAPI topics, where you can do some magic tricks.
At the beginning of each chapter, we will indicate to which level the topic belongs.
This book doesn't cover Razor Pages, SignalR, gRPC, and Blazor.
The reason is that gRPC and SignalR are already very specialized, and don't really need to be customized. Blazor is a new member of the ASP.NET Core family and is not widely used yet. Also, the author isn't familiar enough with Blazor to know all the screws to customize it. Razor Pages is on top of the MVC framework and customizations for MVC are also valid for Razor Pages.
This book is for web developers working with ASP.NET Core, who might need to change default behaviors to get things done. Readers should have basic knowledge of ASP. NET Core and C#, since this book does not cover the basics of those technologies. Readers should also have a good knowledge of Visual Studio, Visual Studio Code, or any other code editor that supports ASP.NET Core and C#.
Chapter 1, Customizing Logging, teaches you how to customize the logging behavior and how to add a custom logging provider.
Chapter 2, Customizing App Configuration, helps you understand how to use different configuration sources and add custom configuration providers.
Chapter 3, Customizing Dependency Injection, teaches you how Dependency Injection (DI) works and how to use a different DI container.
Chapter 4, Configuring and Customizing HTTPS with Kestrel, looks into configuring HTTPS differently.
Chapter 5, Configuring WebHostBuilder, helps you understand how to set up configuration on the hosting layer.
Chapter 6, Using Different Hosting Models, teaches you about different types of hosting on different platforms.
Chapter 7, Using IHostedService and BackgroundService, makes you understand how to execute tasks in the background.
Chapter 8, Writing Custom Middleware, deals with the HTTP context using middleware.
Chapter 9, Working with Endpoint Routing, helps you understand how to use the new routing to provide custom endpoints.
Chapter 10, Customizing ASP.NET Core Identity, explains how to extend the application's user properties and helps you to change the Identity UI.
Chapter 11, Configuring Identity Management, helps you to manage your users and their roles.
Chapter 12, Content Negotiation Using a Custom OutputFormatter, teaches you how to output different content types based on the HTTP Accept header.
Chapter 13, Managing Inputs with Custom ModelBinder, helps you create input models with different types of content.
Chapter 14, Creating a Custom ActionFilter, covers aspect-oriented programming using ActionFilter.
Chapter 15, Working with Caches, helps you to make your application faster.
Chapter 16, Creating Custom TagHelper, enables you to simplify the UI layer by creating TagHelper.
Readers should have basic knowledge of ASP.NET Core and C#, as well as Visual Studio, Visual Studio Code, or any other code editor that supports ASP.NET Core and C#.
You should install the latest .NET 6.0 SDK on your machine. Please find the latest version at https://dotnet.microsoft.com/download/dotnet-core/.
Feel free to use any code editor you like that supports ASP.NET Core and C#. We recommend using Visual Studio Code (https://code.visualstudio.com/), which is available on all platforms and is used by the author of this book.
All the projects in this book will be created using a console, Command Prompt, shell, or PowerShell. Feel free to use whatever console you are comfortable with. The author uses Windows Command Prompt, hosted in the cmder shell (https://cmder.net/