Class ConfigurableResponseNegotiator<TOptions>
- Namespace
- Codebelt.Extensions.Carter.Response
- Assembly
- Codebelt.Extensions.Carter.dll
Provides an abstract, configurable base class for Carter response negotiators that serialize models using a StreamFormatter<TOptions>.
public abstract class ConfigurableResponseNegotiator<TOptions> : Configurable<TOptions>, IConfigurable<TOptions>, IResponseNegotiator where TOptions : class, IExceptionDescriptorOptions, IContentNegotiation, IValidatableParameterObject, new()
Type Parameters
TOptionsThe type of the configured options.
- Inheritance
-
Configurable<TOptions>ConfigurableResponseNegotiator<TOptions>
- Implements
-
IConfigurable<TOptions>
- Derived
- Inherited Members
Constructors
ConfigurableResponseNegotiator(TOptions)
Initializes a new instance of the ConfigurableResponseNegotiator<TOptions> class.
protected ConfigurableResponseNegotiator(TOptions options)
Parameters
optionsTOptionsThe
TOptionsused to configure content negotiation and serialization behavior.
Properties
ContentType
Gets the matched content type determined by the most recent successful call to CanHandle(MediaTypeHeaderValue).
public string ContentType { get; }
Property Value
- string
The matched content type media type string, or
nullif CanHandle(MediaTypeHeaderValue) has not yet been called successfully.
Methods
CanHandle(MediaTypeHeaderValue)
Determines whether this negotiator can handle the specified accept media type.
public virtual bool CanHandle(MediaTypeHeaderValue accept)
Parameters
acceptMediaTypeHeaderValueThe MediaTypeHeaderValue from the HTTP request's Accept header.
Returns
- bool
trueif the negotiator can handle the specified media type; otherwise,false.
GetDefaultEncoding()
When overridden in a derived class, returns the default character encoding for this negotiator.
This encoding is used as the fallback by GetEncoding(HttpRequest) when the HTTP request does not
specify a resolvable Accept-Charset preference.
protected abstract Encoding GetDefaultEncoding()
Returns
GetEncoding(HttpRequest)
Resolves the character encoding for the HTTP response by inspecting the Accept-Charset header
of the request, falling back to GetDefaultEncoding() when no
valid charset is indicated.
public virtual Encoding GetEncoding(HttpRequest request)
Parameters
requestHttpRequestThe current HttpRequest.
Returns
GetFormatter()
When overridden in a derived class, returns the StreamFormatter<TOptions> used to serialize the response model.
public abstract StreamFormatter<TOptions> GetFormatter()
Returns
- StreamFormatter<TOptions>
A StreamFormatter<TOptions> configured with the current
TOptions.
Handle<T>(HttpRequest, HttpResponse, T, CancellationToken)
Serializes the specified model and writes it to the HTTP response body.
public virtual Task Handle<T>(HttpRequest req, HttpResponse res, T model, CancellationToken cancellationToken)
Parameters
reqHttpRequestThe current HttpRequest.
resHttpResponseThe current HttpResponse to which the serialized content is written.
modelTThe model to serialize.
cancellationTokenCancellationTokenA CancellationToken used to propagate notification that the operation should be canceled.
Returns
Type Parameters
TThe type of the model to serialize.