Table of Contents

HttpLastModifiedHeaderFilter Class

Definition

A filter that applies a HTTP Last-Modified header.

public class HttpLastModifiedHeaderFilter : IConfigurable<HttpLastModifiedHeaderOptions>, ICacheableAsyncResultFilter, IAsyncResultFilter, IFilterMetadata
Inheritance
HttpLastModifiedHeaderFilter
Implements

Examples

The following example shows how executes OnResultExecutionAsync for an IEntityDataTimestamp result and emits the Last-Modified response header.

// Program.cs
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Cuemon.AspNetCore.Mvc.Filters.Cacheable;
using Cuemon.Data.Integrity;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Abstractions;
using Microsoft.AspNetCore.Mvc.Filters;
using Microsoft.AspNetCore.Routing;
using Microsoft.Net.Http.Headers;
using MyApp.Examples;

await HttpLastModifiedHeaderFilterExample.DemonstrateAsync();

namespace MyApp.Examples
{
    public static class HttpLastModifiedHeaderFilterExample
    {
        public static async Task DemonstrateAsync()
        {
            var filter = new HttpLastModifiedHeaderFilter();

            var httpContext = new DefaultHttpContext();
            httpContext.Request.Method = HttpMethods.Get;

            var actionContext = new ActionContext(httpContext, new RouteData(), new ActionDescriptor());
            var payload = new TimestampedResource(
                new DateTime(2025, 1, 1, 0, 0, 0, DateTimeKind.Utc),
                new DateTime(2025, 1, 2, 12, 0, 0, DateTimeKind.Utc));

            var resultContext = new ResultExecutingContext(
                actionContext,
                new List<IFilterMetadata>(),
                new ObjectResult(payload),
                controller: null!);

            await filter.OnResultExecutionAsync(resultContext, () =>
                Task.FromResult(new ResultExecutedContext(actionContext, new List<IFilterMetadata>(), resultContext.Result, controller: null!)));

            Console.WriteLine($"HasLastModifiedProvider: {filter.Options.HasLastModifiedProvider}");
            Console.WriteLine($"Last-Modified: {httpContext.Response.Headers[HeaderNames.LastModified]}");
        }

        private sealed record TimestampedResource(DateTime Created, DateTime? Modified) : IEntityDataTimestamp;
    }
}

Constructors

Name Description
HttpLastModifiedHeaderFilter(Action<HttpLastModifiedHeaderOptions>)

Initializes a new instance of the HttpLastModifiedHeaderFilter class.

Properties

Name Description
Options

Gets the configured options of this instance.

Methods

Name Description
OnResultExecutionAsync(ResultExecutingContext, ResultExecutionDelegate)

Called asynchronously before the action result.

See Also