/// <inheritdoc /> public virtual bool CanWriteResult(OutputFormatterContext context, MediaTypeHeaderValue contentType) { MediaTypeHeaderValue mediaType = null; if (contentType == null) { // If the desired content type is set to null, the current formatter is free to choose the // response media type. mediaType = SupportedMediaTypes.FirstOrDefault(); } else { // Since supportedMedia Type is going to be more specific check if supportedMediaType is a subset // of the content type which is typically what we get on acceptHeader. mediaType = SupportedMediaTypes .FirstOrDefault(supportedMediaType => supportedMediaType.IsSubsetOf(contentType)); } if (mediaType != null) { context.SelectedContentType = mediaType; return true; } return false; }
public override bool CanWriteResult(OutputFormatterContext context, MediaTypeHeaderValue contentType) { // Ignore the passed in content type, if the object is string // always return it as a text/plain format. if (context.DeclaredType == typeof(string)) { return true; } if (context.Object is string) { return true; } return false; }
public override bool CanWriteResult(OutputFormatterContext context, MediaTypeHeaderValue contentType) { // Do not set the selected media Type. // The WriteResponseContentHeader should do it for you. return true; }
public static MediaTypeHeaderValue Parse(string input) { if (string.IsNullOrEmpty(input)) { return null; } var inputArray = input.Split(new[] { ';' }, 2); var mediaTypeParts = inputArray[0].Split('/'); if (mediaTypeParts.Length != 2) { return null; } // TODO: throw if the media type and subtypes are invalid. var mediaType = mediaTypeParts[0].Trim(); var mediaSubType = mediaTypeParts[1].Trim(); var mediaTypeRange = MediaTypeHeaderValueRange.None; if (mediaType == "*" && mediaSubType == "*") { mediaTypeRange = MediaTypeHeaderValueRange.AllMediaRange; } else if (mediaSubType == "*") { mediaTypeRange = MediaTypeHeaderValueRange.SubtypeMediaRange; } Dictionary<string, string> parameters = null; string charset = null; if (inputArray.Length == 2) { parameters = ParseParameters(inputArray[1]); parameters.TryGetValue("charset", out charset); } var mediaTypeHeader = new MediaTypeHeaderValue() { MediaType = mediaType, MediaSubType = mediaSubType, MediaTypeRange = mediaTypeRange, Charset = charset, Parameters = parameters ?? new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase), }; return mediaTypeHeader; }
/// <summary> /// Determines whether this instance is a subset of passed <see cref="MediaTypeHeaderValue"/>. /// If the media type and media type parameters of this media type are all present /// and match those of <paramref name="otherMediaType"/> then it is a match even though /// <paramref name="otherMediaType"/> may have additional parameters. /// </summary> /// <param name="mediaType">The first media type.</param> /// <param name="otherMediaType">The second media type.</param> /// <returns><c>true</c> if this is a subset of <paramref name="otherMediaType"/>; false otherwise.</returns> public bool IsSubsetOf(MediaTypeHeaderValue otherMediaType) { if (otherMediaType == null) { return false; } if (!MediaType.Equals(otherMediaType.MediaType, StringComparison.OrdinalIgnoreCase)) { if (otherMediaType.MediaTypeRange != MediaTypeHeaderValueRange.AllMediaRange) { return false; } } else if (!MediaSubType.Equals(otherMediaType.MediaSubType, StringComparison.OrdinalIgnoreCase)) { if (otherMediaType.MediaTypeRange != MediaTypeHeaderValueRange.SubtypeMediaRange) { return false; } } if (Parameters != null) { if (Parameters.Count != 0 && (otherMediaType.Parameters == null || otherMediaType.Parameters.Count == 0)) { return false; } // So far we either have a full match or a subset match. Now check that all of // mediaType1's parameters are present and equal in mediatype2 if (!MatchParameters(Parameters, otherMediaType.Parameters)) { return false; } } return true; }
public virtual bool CanWriteResult(OutputFormatterContext context, MediaTypeHeaderValue contentType) { return false; }