Customizing ASP.NET Core 6.0
eBook - ePub

Customizing ASP.NET Core 6.0

Learn to turn the right screws to optimize ASP.NET Core applications for better performance, 2nd Edition

  1. 202 pages
  2. English
  3. ePUB (mobile friendly)
  4. Available on iOS & Android
eBook - ePub

Customizing ASP.NET Core 6.0

Learn to turn the right screws to optimize ASP.NET Core applications for better performance, 2nd Edition

About this book

Explore hidden behaviors and customization techniques to help you get the most out of ASP.NET Core for building web applications

Key Features

  • Second edition updated and enhanced to cover the latest.NET 6 features and changes
  • Learn expert techniques to implement authentication and authorization for securing your web apps
  • Discover best practices for configuring ASP.NET Core, from user interface design to hosting it on platforms

Book Description

ASP.NET Core comes packed full of hidden features for building sophisticated web applications. You'd be missing out on a lot of its capabilities by not customizing it to work for your applications. With Customizing ASP.NET Core 6.0, you'll discover techniques to help you get the most out of the framework to deliver robust applications.

In this updated second edition, you'll cover the latest features and changes in the.NET 6 LTS version. You'll find new insights and customization techniques for important topics such as authentication and authorization. The book will also show you how to work with caches and change the default behavior of ASP.NET Core apps. You'll learn essential concepts relating to optimizing 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 robust optimized apps.

What you will learn

  • Explore various application configurations and providers in ASP.NET Core 6
  • Enable and work with caches to improve the performance of your application
  • Understand dependency injection in.NET and learn how to add third-party DI containers
  • Discover the concept of middleware and write your middleware for ASP.NET Core apps
  • Create various API output formats in your API-driven projects
  • Get familiar with different hosting models for your ASP.NET Core app

Who this book is for

This.NET 6 book is for.NET developers who need to change the default behaviors of the framework to help improve the performance of their applications. Intermediate-level knowledge of ASP.NET Core and C# is required before getting started with the book.

Frequently asked questions

Yes, you can cancel anytime from the Subscription tab in your account settings on the Perlego website. Your subscription will stay active until the end of your current billing period. Learn how to cancel your subscription.
At the moment all of our mobile-responsive ePub books are available to download via the app. Most of our PDFs are also available to download and we're working on making the final remaining ones downloadable now. Learn more here.
Perlego offers two plans: Essential and Complete
  • Essential is ideal for learners and professionals who enjoy exploring a wide range of subjects. Access the Essential Library with 800,000+ trusted titles and best-sellers across business, personal growth, and the humanities. Includes unlimited reading time and Standard Read Aloud voice.
  • Complete: Perfect for advanced learners and researchers needing full, unrestricted access. Unlock 1.4M+ books across hundreds of subjects, including academic and specialized titles. The Complete Plan also includes advanced features like Premium Read Aloud and Research Assistant.
Both plans are available with monthly, semester, or annual billing cycles.
We are an online textbook subscription service, where you can get access to an entire online library for less than the price of a single book per month. With over 1 million books across 1000+ topics, we’ve got you covered! Learn more here.
Look out for the read-aloud symbol on your next book to see if you can listen to it. The read-aloud tool reads text aloud for you, highlighting the text as it is being read. You can pause it, speed it up and slow it down. Learn more here.
Yes! You can use the Perlego app on both iOS or Android devices to read anytime, anywhere — even offline. Perfect for commutes or when you’re on the go.
Please note we cannot support devices running on iOS 13 and Android 7 or earlier. Learn more about using the app.
Yes, you can access Customizing ASP.NET Core 6.0 by Jürgen Gutsch in PDF and/or ePUB format, as well as other popular books in Computer Science & Microsoft Programming. We have over one million books available in our catalogue for you to explore.

Information

Chapter 1: Customizing Logging

In this chapter, the first in this book about customizing ASP.NET Core, you will see how to customize logging. The default logging only writes to the console or the debug window. This is quite good for the majority of cases, but sometimes you need to log to a sink, such as a file or a database. Or, perhaps you want to extend the logger with additional information. In these cases, you need to know how to change the default logging.
In this chapter, we will be covering the following topics:
  • Configuring logging
  • Creating a custom logger
  • Plugging in an existing third-party logger provider
The topics in this chapter refer to the hosting layer of the ASP.NET Core architecture:
Figure 1.1 – The ASP.NET Core architecture
Figure 1.1 – The ASP.NET Core architecture

Technical requirements

To follow the descriptions in this chapter, you will need to create an ASP.NET Core MVC application. To do this, open your console, shell, or Bash terminal, and change to your working directory. Then, use the following command to create a new MVC application:
dotnet new mvc -n LoggingSample -o LoggingSample
Now, open the project in Microsoft Visual Studio by double-clicking the project file, or in Visual Studio Code, by typing the following command in the already-open console:
cd LoggingSample
code .
All of the code samples in this chapter can be found in the GitHub repository for this book at https://github.com/PacktPublishing/Customizing-ASP.NET-Core-6.0-Second-Edition/tree/main/Chapter01.

Configuring logging

In previous versions of ASP.NET Core (that is, before version 2.0), logging was configured in Startup.cs. As a reminder, since version 2.0, the Startup.cs file has been simplified, and a lot of configurations have been moved to the default WebHostBuilder, which is called in Program.cs. Also, logging was moved to the default WebHostBuilder.
In ASP.NET Core 3.1 and later versions, the Program.cs file gets more generic, and IHostBuilder will be created first. IHostBuilder is pretty useful for bootstrapping an application without all of the ASP.NET web stuff. We'll learn a lot more about IHostBuilder later on in this book. With this IHostBuilder, we create IWebHostBuilder to configure ASP.NET Core. In ASP.NET Core 3.1 and later versions, we get IWebHostBuilder with the webBuilder variable:
public class Program
{
public static void Main(string[] args)
{
CreateHostBuilder(args).Build().Run();
}
public static IHostBuilder CreateHostBuilder(
string[]args) =>
Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>();
});
}
In ASP.NET Core 6.0, Microsoft introduced the minimal API approach that simplifies the configuration a lot. This approach doesn't use the Startup file and adds all of the configurations to the Program.cs file instead. Let's see what this looks like:
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddControllersWithViews();
var app = builder.Build();
// The rest of the file isn't relevant for this chapter
In ASP.NET Core, you are able to override and customize almost everything. This includes logging. IWebHostBuilder has a lot of extension methods that allow us to override the default behavior of different features. To override the default settings for logging, we need to use the ConfigureLogging method. The following code snippet shows almost exactly the same logging as was configured inside the ConfigureWebHostDefaults() method:
Host.Cre...

Table of contents

  1. Customizing ASP.NET Core 6.0
  2. Second Edition
  3. Preface
  4. Chapter 1: Customizing Logging
  5. Chapter 2: Customizing App Configuration
  6. Chapter 3: Customizing Dependency Injection
  7. Chapter 4: Configuring and Customizing HTTPS with Kestrel
  8. Chapter 5: Configuring WebHostBuilder
  9. Chapter 6: Using Different Hosting Models
  10. Chapter 7: Using IHostedService and BackgroundService
  11. Chapter 8: Writing Custom Middleware
  12. Chapter 9: Working with Endpoint Routing
  13. Chapter 10: Customizing ASP.NET Core Identity
  14. Chapter 11: Configuring Identity Management
  15. Chapter 12: Content Negotiation Using a Custom OutputFormatter
  16. Chapter 13: Managing Inputs with Custom ModelBinder
  17. Chapter 14: Creating a Custom ActionFilter
  18. Chapter 15: Working with Caches
  19. Chapter 16: Creating Custom TagHelper
  20. Other Books You May Enjoy