C# Class MvcApi.Formatting.DefaultContentNegotiator

Class that selects a MediaTypeFormatter for an HttpRequestMessage or HttpResponseMessage.
Inheritance: IContentNegotiator
Show file Open project: dax70/MvcApi

Public Methods

Method Description
DefaultContentNegotiator ( ) : System
DefaultContentNegotiator ( bool excludeMatchOnTypeOnly ) : System

Initializes a new instance of the DefaultContentNegotiator with the given setting for excludeMatchOnTypeOnly.

Negotiate ( Type type, HttpRequestMessage request, IEnumerable formatters ) : ContentNegotiationResult

Performs content negotiating by selecting the most appropriate MediaTypeFormatter out of the passed in formatters for the given request that can serialize an object of the given type.

Protected Methods

Method Description
ComputeFormatterMatches ( Type type, HttpRequestMessage request, IEnumerable formatters ) : Collection

Determine how well each formatter matches by associating a MediaTypeFormatterMatchRanking value with the formatter. Then associate the quality of the match based on q-factors and other parameters. The result of this method is a collection of the matches found categorized and assigned a quality value.

MatchAcceptHeader ( IEnumerable sortedAcceptValues, MediaTypeFormatter formatter ) : MediaTypeFormatterMatch

Match the request accept header field values against the formatter's registered supported media types.

MatchMediaTypeMapping ( HttpRequestMessage request, MediaTypeFormatter formatter ) : MediaTypeFormatterMatch

Match a request against the MediaTypeMappings registered with the formatter.

MatchRequestMediaType ( HttpRequestMessage request, MediaTypeFormatter formatter ) : MediaTypeFormatterMatch

Match any request media type (in case there is a request entity body) against the formatter's registered media types.

MatchType ( Type type, MediaTypeFormatter formatter ) : MediaTypeFormatterMatch

Pick the first supported media type and indicate we've matched only on type If ExcludeMatchOnTypeOnly is true then we don't match on type only which means that we return null if we can't match on anything in the request. This is useful for generating 406 (Not Acceptable) status codes.

SelectResponseCharacterEncoding ( HttpRequestMessage request, MediaTypeFormatter formatter ) : Encoding

Determine the best character encoding for writing the response. First we look for accept-charset headers and if not found then we try to match any charset encoding in the request (in case of PUT, POST, etc.) If no encoding is found then we use the default for the formatter.

SelectResponseMediaTypeFormatter ( ICollection matches ) : MediaTypeFormatterMatch

Select the best match among the candidate matches found.

SortMediaTypeWithQualityHeaderValuesByQFactor ( ICollection headerValues ) : IEnumerable

Sort Accept header values and related header field values with similar syntax rules (if more than 1) in descending order based on q-factor.

SortStringWithQualityHeaderValuesByQFactor ( ICollection headerValues ) : IEnumerable

Sort Accept-Charset, Accept-Encoding, Accept-Language and related header field values with similar syntax rules (if more than 1) in descending order based on q-factor.

UpdateBestMatch ( MediaTypeFormatterMatch current, MediaTypeFormatterMatch potentialReplacement ) : MediaTypeFormatterMatch

Evaluates whether a match is better than the current match and if so returns the replacement; otherwise returns the current match.

Method Details

ComputeFormatterMatches() protected method

Determine how well each formatter matches by associating a MediaTypeFormatterMatchRanking value with the formatter. Then associate the quality of the match based on q-factors and other parameters. The result of this method is a collection of the matches found categorized and assigned a quality value.
protected ComputeFormatterMatches ( Type type, HttpRequestMessage request, IEnumerable formatters ) : Collection
type System.Type The type to be serialized.
request MvcApi.Http.HttpRequestMessage The request.
formatters IEnumerable The set of objects from which to choose.
return Collection

DefaultContentNegotiator() public method

public DefaultContentNegotiator ( ) : System
return System

DefaultContentNegotiator() public method

Initializes a new instance of the DefaultContentNegotiator with the given setting for excludeMatchOnTypeOnly.
public DefaultContentNegotiator ( bool excludeMatchOnTypeOnly ) : System
excludeMatchOnTypeOnly bool /// If ExcludeMatchOnTypeOnly is true then we don't match on type only which means /// that we return null if we can't match on anything in the request. This is useful /// for generating 406 (Not Acceptable) status codes. ///
return System

MatchAcceptHeader() protected method

Match the request accept header field values against the formatter's registered supported media types.
protected MatchAcceptHeader ( IEnumerable sortedAcceptValues, MediaTypeFormatter formatter ) : MediaTypeFormatterMatch
sortedAcceptValues IEnumerable The sorted accept header values to match.
formatter MediaTypeFormatter The formatter to match against.
return MediaTypeFormatterMatch

MatchMediaTypeMapping() protected method

Match a request against the MediaTypeMappings registered with the formatter.
protected MatchMediaTypeMapping ( HttpRequestMessage request, MediaTypeFormatter formatter ) : MediaTypeFormatterMatch
request MvcApi.Http.HttpRequestMessage The request to match.
formatter MediaTypeFormatter The formatter to match against.
return MediaTypeFormatterMatch

MatchRequestMediaType() protected method

Match any request media type (in case there is a request entity body) against the formatter's registered media types.
protected MatchRequestMediaType ( HttpRequestMessage request, MediaTypeFormatter formatter ) : MediaTypeFormatterMatch
request MvcApi.Http.HttpRequestMessage The request to match.
formatter MediaTypeFormatter The formatter to match against.
return MediaTypeFormatterMatch

MatchType() protected method

Pick the first supported media type and indicate we've matched only on type If ExcludeMatchOnTypeOnly is true then we don't match on type only which means that we return null if we can't match on anything in the request. This is useful for generating 406 (Not Acceptable) status codes.
protected MatchType ( Type type, MediaTypeFormatter formatter ) : MediaTypeFormatterMatch
type System.Type The type to be serialized.
formatter MediaTypeFormatter The formatter we are matching against.
return MediaTypeFormatterMatch

Negotiate() public method

Performs content negotiating by selecting the most appropriate MediaTypeFormatter out of the passed in formatters for the given request that can serialize an object of the given type.
public Negotiate ( Type type, HttpRequestMessage request, IEnumerable formatters ) : ContentNegotiationResult
type System.Type The type to be serialized.
request MvcApi.Http.HttpRequestMessage The request.
formatters IEnumerable The set of objects from which to choose.
return ContentNegotiationResult

SelectResponseCharacterEncoding() protected method

Determine the best character encoding for writing the response. First we look for accept-charset headers and if not found then we try to match any charset encoding in the request (in case of PUT, POST, etc.) If no encoding is found then we use the default for the formatter.
protected SelectResponseCharacterEncoding ( HttpRequestMessage request, MediaTypeFormatter formatter ) : Encoding
request MvcApi.Http.HttpRequestMessage
formatter MediaTypeFormatter
return System.Text.Encoding

SelectResponseMediaTypeFormatter() protected method

Select the best match among the candidate matches found.
protected SelectResponseMediaTypeFormatter ( ICollection matches ) : MediaTypeFormatterMatch
matches ICollection The collection of matches.
return MediaTypeFormatterMatch

SortMediaTypeWithQualityHeaderValuesByQFactor() protected method

Sort Accept header values and related header field values with similar syntax rules (if more than 1) in descending order based on q-factor.
protected SortMediaTypeWithQualityHeaderValuesByQFactor ( ICollection headerValues ) : IEnumerable
headerValues ICollection The header values to sort.
return IEnumerable

SortStringWithQualityHeaderValuesByQFactor() protected method

Sort Accept-Charset, Accept-Encoding, Accept-Language and related header field values with similar syntax rules (if more than 1) in descending order based on q-factor.
protected SortStringWithQualityHeaderValuesByQFactor ( ICollection headerValues ) : IEnumerable
headerValues ICollection The header values to sort.
return IEnumerable

UpdateBestMatch() protected method

Evaluates whether a match is better than the current match and if so returns the replacement; otherwise returns the current match.
protected UpdateBestMatch ( MediaTypeFormatterMatch current, MediaTypeFormatterMatch potentialReplacement ) : MediaTypeFormatterMatch
current MediaTypeFormatterMatch
potentialReplacement MediaTypeFormatterMatch
return MediaTypeFormatterMatch