Search icon CANCEL
Subscription
0
Cart icon
Cart
Close icon
You have no products in your basket yet
Save more on your purchases!
Savings automatically calculated. No voucher code required
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletters
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Customizing ASP.NET Core 6.0 - Second Edition

You're reading from  Customizing ASP.NET Core 6.0 - Second Edition

Product type Book
Published in Dec 2021
Publisher Packt
ISBN-13 9781803233604
Pages 204 pages
Edition 2nd Edition
Languages
Author (1):
Jürgen Gutsch Jürgen Gutsch
Profile icon Jürgen Gutsch
Toc

Table of Contents (18) Chapters close

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

Using typed configurations

Before trying to read INI files, it makes sense for you to see how to use typed configurations instead of reading the configuration via IConfiguration, key by key.

To read a typed configuration, you need to define the type to configure. I usually create a class called AppSettings, as follows:

namespace ConfigureSample;
public class AppSettings
{
    public int Foo { get; set; }
    public string Bar { get; set; }
}

This is a simple Plain Old CLR Object (POCO) class that will only contain the application setting values, as illustrated in the following code snippet. These classes can then be filled with specific configuration sections inside the ConfigureServices method in Startup.cs until ASP.NET Core 5.0:

services.Configure<AppSettings>
   (Configuration.GetSection("AppSettings"));

Using the minimal API approach, you need to configure the AppSettings class, like this:

builder.Services.Configure<AppSettings>(
    builder.Configuration.GetSection("AppSettings"));

This way, the typed configuration also gets registered as a service in the dependency injection (DI) container and can be used everywhere in the application. You are able to create different configuration types for each configuration section. In most cases, one section should be fine, but sometimes it makes sense to divide the settings into different sections. The next snippet shows how to use the configuration in an MVC controller:

using Microsoft.Extensions.Options;
// ...
public class HomeController : Controller
{
    private readonly AppSettings _options;
    public HomeController(IOptions<AppSettings> options)
    {
        _options = options.Value;
    }
    public IActionResult Index()
    {
        ViewData["Message"] = _options.Bar;
        return View();
    }

IOptions<AppSettings> is a wrapper around our AppSettings type, and the Value property contains the actual instance of AppSettings, including the values from the configuration file.

To try reading the settings in, the appsettings.json file needs to have the AppSettings section configured, otherwise the values are null or not set. Let's now add the section to the appsettings.json file, as follows:

{
    "Logging": {
        "LogLevel": {
            "Default": "Warning"
        }
    },
    "AllowedHosts": "*",
    "AppSettings": {
        "Foo": 123,
        "Bar": "Bar"
    }
}

Next, we'll examine how INI files can be used for configuration.

You have been reading a chapter from
Customizing ASP.NET Core 6.0 - Second Edition
Published in: Dec 2021 Publisher: Packt ISBN-13: 9781803233604
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime}