Table of Contents

Class HttpStatusCodeExceptionDecoratorExtensions

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

Extension methods for the HttpStatusCodeException class hidden behind the IDecorator<T> interface.

public static class HttpStatusCodeExceptionDecoratorExtensions
Inheritance
HttpStatusCodeExceptionDecoratorExtensions

Examples

The following example demonstrates how to add response headers to an HttpStatusCodeException using the decorator pattern.

using System.Net.Http;
using System.Net.Http.Headers;
using Cuemon.AspNetCore.Http;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Primitives;

using Cuemon;
namespace Examples;

public class HttpStatusCodeExceptionHeadersExample
{
    public void AddHeadersToException()
    {
        var exception = new NotFoundException("Resource not found.");
        var headers = new HeaderDictionary
        {
            { "X-Correlation-Id", new StringValues("abc-123") },
            { "X-Request-Id", new StringValues("req-456") }
        };

        Decorator.Enclose(exception).AddResponseHeaders(headers);

        using var message = new HttpResponseMessage();
        message.Headers.Add("X-Server-Id", "server-01");

        Decorator.Enclose(exception).AddResponseHeaders(message.Headers);

}
}

Methods

AddResponseHeaders<T>(IDecorator<T>, IHeaderDictionary)

Adds all non-existing headers to the enclosed HttpStatusCodeException of the decorator.

public static IDecorator<T> AddResponseHeaders<T>(this IDecorator<T> decorator, IHeaderDictionary headers) where T : HttpStatusCodeException

Parameters

decorator IDecorator<T>

The IDecorator<T> to extend.

headers IHeaderDictionary

The IHeaderDictionary to populate into the enclosed HttpStatusCodeException.

Returns

IDecorator<T>

Type Parameters

T

Exceptions

ArgumentNullException

decorator cannot be null -or- headers cannot be null.

AddResponseHeaders<T>(IDecorator<T>, HttpResponseHeaders)

Adds all non-existing headers to the enclosed HttpStatusCodeException of the decorator.

public static IDecorator<T> AddResponseHeaders<T>(this IDecorator<T> decorator, HttpResponseHeaders headers) where T : HttpStatusCodeException

Parameters

decorator IDecorator<T>

The IDecorator<T> to extend.

headers HttpResponseHeaders

The HttpResponseHeaders to populate into the enclosed HttpStatusCodeException.

Returns

IDecorator<T>

Type Parameters

T

Exceptions

ArgumentNullException

decorator cannot be null -or- headers cannot be null.

See Also