Beispiel #1
0
        /// <summary>
        /// Sets the default headers for content that will be formatted using this formatter. This method
        /// is called from the <see cref="ObjectContent"/> constructor.
        /// This implementation sets the Content-Type header to the value of <paramref name="mediaType"/> if it is
        /// not <c>null</c>. If it is <c>null</c> it sets the Content-Type to the default media type of this formatter.
        /// If the Content-Type does not specify a charset it will set it using this formatters configured
        /// <see cref="Encoding"/>.
        /// </summary>
        /// <remarks>
        /// Subclasses can override this method to set content headers such as Content-Type etc. Subclasses should
        /// call the base implementation. Subclasses should treat the passed in <paramref name="mediaType"/> (if not <c>null</c>)
        /// as the authoritative media type and use that as the Content-Type.
        /// </remarks>
        /// <param name="type">The type of the object being serialized. See <see cref="ObjectContent"/>.</param>
        /// <param name="headers">The content headers that should be configured.</param>
        /// <param name="mediaType">The authoritative media type. Can be <c>null</c>.</param>
        public virtual void SetDefaultContentHeaders(Type type, HttpContentHeaders headers, string mediaType)
        {
            if (type == null)
            {
                throw new ArgumentNullException("type");
            }
            if (headers == null)
            {
                throw new ArgumentNullException("headers");
            }

            if (!String.IsNullOrEmpty(mediaType))
            {
                var parsedMediaType = MediaTypeHeaderValue.Parse(mediaType);
                headers.ContentType = parsedMediaType;
            }

            if (headers.ContentType == null)
            {
                MediaTypeHeaderValue defaultMediaType = SupportedMediaTypes.FirstOrDefault();
                if (defaultMediaType != null)
                {
                    headers.ContentType = defaultMediaType.Clone();
                }
            }
        }
 private void SetMediaTypes()
 {
     if (!allMediaTypes.ContainsKey(SelectedOutputFormat.Guid))
     {
         TryGetSupportedMediaTypes();
     }
     FilterSupportedMediaTypes();
     OnPropertyChanged("SupportedMediaTypes");
     SelectedMediaType = SupportedMediaTypes.FirstOrDefault();
 }
        /// <summary>
        /// Sets the default headers for content that will be formatted using this formatter. This method
        /// is called from the <see cref="ObjectContent"/> constructor.
        /// This implementation sets the Content-Type header to the value of <paramref name="mediaType"/> if it is
        /// not <c>null</c>. If it is <c>null</c> it sets the Content-Type to the default media type of this formatter.
        /// If the Content-Type does not specify a charset it will set it using this formatters configured
        /// <see cref="Encoding"/>.
        /// </summary>
        /// <remarks>
        /// Subclasses can override this method to set content headers such as Content-Type etc. Subclasses should
        /// call the base implementation. Subclasses should treat the passed in <paramref name="mediaType"/> (if not <c>null</c>)
        /// as the authoritative media type and use that as the Content-Type.
        /// </remarks>
        /// <param name="type">The type of the object being serialized. See <see cref="ObjectContent"/>.</param>
        /// <param name="headers">The content headers that should be configured.</param>
        /// <param name="mediaType">The authoritative media type. Can be <c>null</c>.</param>
        public virtual void SetDefaultContentHeaders(Type type, HttpContentHeaders headers, string mediaType)
        {
            if (type == null)
            {
                throw new ArgumentNullException("type");
            }
            if (headers == null)
            {
                throw new ArgumentNullException("headers");
            }

            if (!String.IsNullOrEmpty(mediaType))
            {
                var parsedMediaType = MediaTypeHeaderValue.Parse(mediaType);
                headers.ContentType = parsedMediaType;
            }

            // If content type is not set then set it based on supported media types.
            if (headers.ContentType == null)
            {
                MediaTypeHeaderValue defaultMediaType = SupportedMediaTypes.FirstOrDefault();
                if (defaultMediaType != null)
                {
                    headers.ContentType = defaultMediaType.Clone();
                }
            }

            // If content type charset parameter is not set then set it based on the supported encodings.
            if (headers.ContentType != null && headers.ContentType.CharSet == null)
            {
                Encoding defaultEncoding = SupportedEncodings.FirstOrDefault();
                if (defaultEncoding != null)
                {
                    headers.ContentType.CharSet = defaultEncoding.WebName;
                }
            }
        }
        /// <summary>
        ///     Sets the default headers for content that will be formatted using this formatter. This method
        ///     is called from the <see cref="ObjectContent" /> constructor.
        ///     This implementation sets the Content-Type header to the value of <paramref name="mediaType" /> if it is
        ///     not <c>null</c>. If it is <c>null</c> it sets the Content-Type to the default media type of this formatter.
        ///     If the Content-Type does not specify a charset it will set it using this formatters configured
        ///     <see cref="Encoding" />.
        /// </summary>
        /// <remarks>
        ///     Subclasses can override this method to set content headers such as Content-Type etc. Subclasses should
        ///     call the base implementation. Subclasses should treat the passed in <paramref name="mediaType" /> (if not
        ///     <c>null</c>)
        ///     as the authoritative media type and use that as the Content-Type.
        /// </remarks>
        /// <param name="type">The type of the object being serialized. See <see cref="ObjectContent" />.</param>
        /// <param name="headers">The content headers that should be configured.</param>
        /// <param name="mediaType">The authoritative media type. Can be <c>null</c>.</param>
        public virtual void SetDefaultContentHeaders(Type type, HttpContentHeaders headers,
                                                     MediaTypeHeaderValue?mediaType)
        {
            if (type == null)
            {
                throw Error.ArgumentNull(nameof(type));
            }
            if (headers == null)
            {
                throw Error.ArgumentNull(nameof(headers));
            }

            if (mediaType != null)
            {
                headers.ContentType = (MediaTypeHeaderValue)((ICloneable)mediaType).Clone();
            }

            // If content type is not set then set it based on supported media types.
            if (headers.ContentType == null)
            {
                var defaultMediaType = SupportedMediaTypes.FirstOrDefault();
                if (defaultMediaType != null)
                {
                    headers.ContentType = (MediaTypeHeaderValue)((ICloneable)defaultMediaType).Clone();
                }
            }

            // If content type charset parameter is not set then set it based on the supported encodings.
            if (headers.ContentType != null && headers.ContentType.CharSet == null)
            {
                var defaultEncoding = SupportedEncodings.FirstOrDefault();
                if (defaultEncoding != null)
                {
                    headers.ContentType.CharSet = defaultEncoding.WebName;
                }
            }
        }
        internal ResponseMediaTypeMatch SelectResponseMediaType(Type type, HttpRequestMessage request)
        {
            if (type == null)
            {
                throw new ArgumentNullException("type");
            }

            if (request == null)
            {
                throw new ArgumentNullException("request");
            }

            if (!CanWriteType(type))
            {
                return(null);
            }

            // Determine the best character encoding if we have any registered encoders.
            // Note that it is ok for a formatter not to register any encoders in case it doesn't
            // do any structured reading or writing.
            Encoding characterEncodingMatch = SupportedEncodings.Any() ? SelectResponseCharacterEncoding(request) : null;

            // Determine the best media type
            MediaTypeMatch mediaTypeMatch = null;

            // Match against media type mapping first
            if (TryMatchMediaTypeMapping(request, out mediaTypeMatch))
            {
                mediaTypeMatch.SetEncoding(characterEncodingMatch);
                return(new ResponseMediaTypeMatch(
                           mediaTypeMatch,
                           ResponseFormatterSelectionResult.MatchOnRequestWithMediaTypeMapping));
            }

            // Match against the accept header.
            if (TryMatchSupportedMediaType(request, out mediaTypeMatch))
            {
                mediaTypeMatch.SetEncoding(characterEncodingMatch);
                return(new ResponseMediaTypeMatch(
                           mediaTypeMatch,
                           ResponseFormatterSelectionResult.MatchOnRequestAcceptHeader));
            }

            // Match against request's content type
            HttpContent requestContent = request.Content;

            if (requestContent != null)
            {
                MediaTypeHeaderValue requestContentType = requestContent.Headers.ContentType;
                if (requestContentType != null && TryMatchSupportedMediaType(requestContentType, out mediaTypeMatch))
                {
                    mediaTypeMatch.SetEncoding(characterEncodingMatch);
                    return(new ResponseMediaTypeMatch(
                               mediaTypeMatch,
                               ResponseFormatterSelectionResult.MatchOnRequestContentType));
                }
            }

            // No match at all.
            // Pick the first supported media type and indicate we've matched only on type
            MediaTypeHeaderValue mediaType = SupportedMediaTypes.FirstOrDefault();

            mediaTypeMatch = new MediaTypeMatch(mediaType);
            mediaTypeMatch.SetEncoding(characterEncodingMatch);
            return(new ResponseMediaTypeMatch(
                       mediaTypeMatch,
                       ResponseFormatterSelectionResult.MatchOnCanWriteType));
        }