Class HeaderDictionaryDecoratorExtensions
- Namespace
- Cuemon.AspNetCore.Http
- Assembly
- Cuemon.AspNetCore.dll
Extension methods for the IHeaderDictionary interface hidden behind the IDecorator<T> interface.
public static class HeaderDictionaryDecoratorExtensions
- Inheritance
-
HeaderDictionaryDecoratorExtensions
Examples
HeaderDictionaryDecoratorExtensions provides extension methods on Decorator.Enclose for merging, sanitizing, and updating IHeaderDictionary instances. This example creates a target HeaderDictionary with "X-Existing" and a source with both "X-Existing" and "X-New", then calls AddRange to selectively add only non-existing headers. It also demonstrates AddOrUpdateHeader with control-character sanitization (removing \r\n from a value) and AddOrUpdateHeaders to copy HttpResponseMessage response headers into the target. Console output confirms "X-Existing" retains its original value, "X-New" is added, sanitized headers are cleaned to "helloworld", and response headers are merged as "alpha,beta".
using System;
using System.Net.Http;
using Cuemon;
using Cuemon.AspNetCore.Http;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Primitives;
namespace MyApp.AspNetCore.Http
{
public class HeaderDictionaryDecoratorExtensionsExample
{
public void Demonstrate()
{
// Add non-existing headers from one dictionary into another
var target = new HeaderDictionary
{
{ "X-Existing", "value1" }
};
var source = new HeaderDictionary
{
{ "X-Existing", "value1" },
{ "X-New", "value2" }
};
// Only adds headers that do not already exist in target
Decorator.Enclose<IHeaderDictionary>(target).AddRange(source);
Console.WriteLine(target["X-Existing"]); // "value1"
Console.WriteLine(target["X-New"]); // "value2"
// Add or update a single header with control-character sanitization
Decorator.Enclose<IHeaderDictionary>(target).AddOrUpdateHeader(
"X-Sanitized", new StringValues("hello\r\nworld"), useAsciiEncodingConversion: false);
Console.WriteLine(target["X-Sanitized"]); // "helloworld"
// Copy response headers into an IHeaderDictionary
using var response = new HttpResponseMessage();
response.Headers.Add("X-Custom", new[] { "alpha", "beta" });
Decorator.Enclose<IHeaderDictionary>(target).AddOrUpdateHeaders(response.Headers);
Console.WriteLine(target["X-Custom"]); // "alpha,beta"
}}
}
Methods
AddOrUpdateHeader(IDecorator<IHeaderDictionary>, string, StringValues, bool)
Attempts to add or update an existing element with the provided key and value to the enclosed IHeaderDictionary of the decorator.
public static void AddOrUpdateHeader(this IDecorator<IHeaderDictionary> decorator, string key, StringValues value, bool useAsciiEncodingConversion = true)
Parameters
decoratorIDecorator<IHeaderDictionary>The IDecorator<T> to extend.
keystringThe string to use as the key of the element to add.
valueStringValuesThe string to use as the value of the element to add.
useAsciiEncodingConversionboolif set to
truean ASCII encoding conversion is applied to thevalue.
Exceptions
- ArgumentNullException
decoratorcannot be null.
AddOrUpdateHeaders(IDecorator<IHeaderDictionary>, HttpResponseHeaders)
Attempts to add or update one or more elements from the provided collection of responseHeaders to the enclosed IHeaderDictionary of the decorator.
public static void AddOrUpdateHeaders(this IDecorator<IHeaderDictionary> decorator, HttpResponseHeaders responseHeaders)
Parameters
decoratorIDecorator<IHeaderDictionary>The IDecorator<T> to extend.
responseHeadersHttpResponseHeadersThe HttpResponseHeaders to copy.
AddRange(IDecorator<IHeaderDictionary>, IHeaderDictionary, Func<KeyValuePair<string, StringValues>, IHeaderDictionary, bool>)
Adds a range of headers to the enclosed IHeaderDictionary.
public static IHeaderDictionary AddRange(this IDecorator<IHeaderDictionary> decorator, IHeaderDictionary headers, Func<KeyValuePair<string, StringValues>, IHeaderDictionary, bool> predicate = null)
Parameters
decoratorIDecorator<IHeaderDictionary>The IDecorator<T> to extend.
headersIHeaderDictionaryThe IHeaderDictionary to populate.
predicateFunc<KeyValuePair<string, StringValues>, IHeaderDictionary, bool>The function delegate that specifies what elements to populate from
headers. Default is only non-existing headers.
Returns
- IHeaderDictionary
A reference to
decorator.Inner.Innerso that additional calls can be chained.
Remarks
When predicate is null, only new headers are added to the enclosed IHeaderDictionary.