Table of Contents

VaryAcceptMiddleware Class

Definition

Middleware that appends Vary: Accept to every response, signalling to HTTP caches (and clients) that the representation varies based on the Accept request header (RFC 9110 ยง12.5.5).

public class VaryAcceptMiddleware : Middleware
Inheritance
VaryAcceptMiddleware
Inherited Members

Examples

The following example demonstrates how to register the to append a Vary: Accept header to every response.

using System;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.Net.Http.Headers;

        namespace Cuemon.AspNetCore.Http.Headers;

        public static class VaryAcceptMiddlewareExample
        {
            public static async Task DemonstrateAsync()
            {
                var context = new DefaultHttpContext();
        var middleware = new VaryAcceptMiddleware(httpContext => httpContext.Response.WriteAsync("Hello"));

        await middleware.InvokeAsync(context);
        Console.WriteLine(context.Response.Headers[HeaderNames.Vary].ToString());
            }
        }

Constructors

Name Description
VaryAcceptMiddleware(RequestDelegate)

Initializes a new instance of the VaryAcceptMiddleware class.

Methods

Name Description
InvokeAsync(HttpContext)

Invokes the middleware for the given HttpContext. Adds the Vary: Accept response header just before the response starts.