Table of Contents

Class CacheableMiddleware

Namespace
Cuemon.AspNetCore.Http.Headers
Assembly
Cuemon.AspNetCore.dll

Provides a Cache-Control middleware implementation for ASP.NET Core.

public class CacheableMiddleware : ConfigurableMiddleware<CacheableOptions>, IConfigurable<CacheableOptions>
Inheritance
CacheableMiddleware
Implements
Inherited Members

Examples

The following example demonstrates how to register and use CacheableMiddleware in the ASP.NET Core pipeline.

using System;
using Cuemon.AspNetCore.Builder;
using Cuemon.AspNetCore.Http.Headers;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Options;
using Microsoft.Net.Http.Headers;

namespace MyApp.Examples;

public class CacheableMiddlewareExample
{
    public void Configure(IApplicationBuilder app)
    {
        // Standard registration via MiddlewareBuilderFactory (recommended)
        MiddlewareBuilderFactory.UseConfigurableMiddleware<CacheableMiddleware, CacheableOptions>(app, options =>
        {
            options.CacheControl = new CacheControlHeaderValue
            {
                Public = true,
                MaxAge = TimeSpan.FromDays(1)
            };
            options.Expires = new ExpiresHeaderValue(TimeSpan.FromDays(1));
        });
    }

    public void DirectInstantiation(RequestDelegate next, IOptions<CacheableOptions> options)
    {
        // Direct usage of the CacheableMiddleware type
        var middleware = new CacheableMiddleware(next, options);
        Console.WriteLine($"Middleware created (CacheControl: {options.Value.UseCacheControl})");
    }
}

Constructors

CacheableMiddleware(RequestDelegate, IOptions<CacheableOptions>)

Initializes a new instance of the CacheableMiddleware class.

public CacheableMiddleware(RequestDelegate next, IOptions<CacheableOptions> setup)

Parameters

next RequestDelegate

The delegate of the request pipeline to invoke.

setup IOptions<CacheableOptions>

The CacheableOptions which need to be configured.

CacheableMiddleware(RequestDelegate, Action<CacheableOptions>)

Initializes a new instance of the CacheableMiddleware class.

public CacheableMiddleware(RequestDelegate next, Action<CacheableOptions> setup)

Parameters

next RequestDelegate

The delegate of the request pipeline to invoke.

setup Action<CacheableOptions>

The CacheableOptions which need to be configured.

Methods

InvokeAsync(HttpContext)

Executes the CacheableMiddleware.

public override Task InvokeAsync(HttpContext context)

Parameters

context HttpContext

The context of the current request.

Returns

Task

A Task that represents the execution of this middleware.