Table of Contents

HttpExceptionDescriptorResponseHandlerDecoratorExtensions Class

Definition

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

public static class HttpExceptionDescriptorResponseHandlerDecoratorExtensions
Inheritance
object
HttpExceptionDescriptorResponseHandlerDecoratorExtensions

Examples

HttpExceptionDescriptorResponseHandlerDecoratorExtensions provides extension methods on Decorator.Enclose for registering additional response handlers that control how HTTP exception descriptors are serialized and returned. This example creates an initial HttpExceptionDescriptorResponseHandler for application/json with a 500 Internal Server Error status, wraps a List<HttpExceptionDescriptorResponseHandler> containing it, and calls AddResponseHandler with an options delegate specifying a custom content type, ContentFactory, and StatusCodeFactory. After execution, the list contains both handlers configured for different response scenarios in the error pipeline.

using System.Collections.Generic;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using Cuemon;
using Cuemon.AspNetCore.Diagnostics;

namespace MyApp.Examples;

public class HttpExceptionDescriptorResponseHandlerDecoratorExtensionsExample
{
    public void Demonstrate()
    {
        var handler = new HttpExceptionDescriptorResponseHandler(
            new MediaTypeHeaderValue("application/json"),
            ed => new HttpResponseMessage(HttpStatusCode.InternalServerError)
            {
                Content = new StringContent(ed.Message)
            });
        var list = new List<HttpExceptionDescriptorResponseHandler> { handler };

        Decorator.Enclose(list).AddResponseHandler(o =>
        {
            o.ContentType = new MediaTypeHeaderValue("application/json");
            o.ContentFactory = ed => new StringContent(ed.Message);
            o.StatusCodeFactory = ed => HttpStatusCode.InternalServerError;
        });
    }
}

Methods

Name Description
AddResponseHandler(IDecorator<ICollection<HttpExceptionDescriptorResponseHandler>>, Action<HttpExceptionDescriptorResponseHandlerOptions>)

Adds an HttpExceptionDescriptorResponseHandler to the underlying list of HttpExceptionDescriptorResponseHandler from the specified the decorator.

See Also