HttpExceptionDescriptorResponseFormatter<TOptions> Class
Definition
- Namespace
- Cuemon.AspNetCore.Diagnostics
- Assemblies
- Cuemon.AspNetCore.dll
Provides a generic way to support content negotiation for exceptions in the application.
public class HttpExceptionDescriptorResponseFormatter<TOptions> : Configurable<TOptions>, IConfigurable<TOptions>, IHttpExceptionDescriptorResponseFormatter where TOptions : class, IContentNegotiation, IParameterObject, new()
Type Parameters
TOptionsThe type of the options to configure.
- Inheritance
-
Configurable<TOptions>HttpExceptionDescriptorResponseFormatter<TOptions>
- Implements
-
IConfigurable<TOptions>
- Inherited Members
Examples
The following example demonstrates how to use to support content negotiation for exceptions.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Net.Http.Headers;
using Cuemon.Configuration;
using Cuemon.Net.Http;
namespace Cuemon.AspNetCore.Diagnostics;
public static class HttpExceptionDescriptorResponseFormatterExample
{
public static void Demonstrate()
{
var formatter = new HttpExceptionDescriptorResponseFormatter<SampleFormatterOptions>(_ => { });
formatter.Populate((exceptionDescriptor, mediaType) =>
new StringContent($"{exceptionDescriptor.StatusCode}:{mediaType.MediaType}"));
var exceptionDescriptor = new HttpExceptionDescriptor(
new InvalidOperationException("boom"),
418,
"Teapot",
"Short and stout");
using var response = formatter.ExceptionDescriptorHandlers.First().ToHttpResponseMessage(exceptionDescriptor);
Console.WriteLine(response.Content.ReadAsStringAsync().GetAwaiter().GetResult());
}
private sealed class SampleFormatterOptions : IContentNegotiation, IParameterObject
{
public IReadOnlyCollection<MediaTypeHeaderValue> SupportedMediaTypes { get; } =
new[] { new MediaTypeHeaderValue("text/plain") };
}
}
Constructors
| Name | Description |
|---|---|
| HttpExceptionDescriptorResponseFormatter(IOptions<TOptions>) | Initializes a new instance of the HttpExceptionDescriptorResponseFormatter<TOptions> class. |
| HttpExceptionDescriptorResponseFormatter(Action<TOptions>) | Initializes a new instance of the HttpExceptionDescriptorResponseFormatter<TOptions> class. |
| HttpExceptionDescriptorResponseFormatter(TOptions) | Initializes a new instance of the HttpExceptionDescriptorResponseFormatter<TOptions> class. |
Properties
| Name | Description |
|---|---|
| ExceptionDescriptorHandlers | Gets the collection of populated HttpExceptionDescriptorResponseHandler instances. |
Methods
| Name | Description |
|---|---|
| Adjust(Action<TOptions>) | Adjusts the Options to your liking. |
| Populate(Func<HttpExceptionDescriptor, MediaTypeHeaderValue, HttpContent>, ICollection<HttpExceptionDescriptorResponseHandler>) | Populates the underlying SupportedMediaTypes to the ExceptionDescriptorHandlers using the specified |