/// <inheritdoc/> public override void SetDefaultContentHeaders(Type type, HttpContentHeaders headers, MediaTypeHeaderValue mediaType) { // Determine the content type or let base class handle it. MediaTypeHeaderValue newMediaType = null; if (ODataOutputFormatterHelper.TryGetContentHeader(type, mediaType, out newMediaType)) { headers.ContentType = newMediaType; } else { // This is the case when a user creates a new ObjectContent<T> passing in a null mediaType base.SetDefaultContentHeaders(type, headers, mediaType); } // Set the character set. IEnumerable <string> acceptCharsetValues = Request.Headers.AcceptCharset.Select(cs => cs.Value); string newCharSet = String.Empty; if (ODataOutputFormatterHelper.TryGetCharSet(headers.ContentType, acceptCharsetValues, out newCharSet)) { headers.ContentType.CharSet = newCharSet; } // Add version header. headers.TryAddWithoutValidation( ODataVersionConstraint.ODataServiceVersionHeader, ODataUtils.ODataVersionToString(ResultHelpers.GetODataResponseVersion(Request))); }
/// <inheritdoc/> public override void WriteResponseHeaders(OutputFormatterWriteContext context) { if (context == null) { throw Error.ArgumentNull("context"); } Type type = context.ObjectType; if (type == null) { throw Error.ArgumentNull("type"); } type = TypeHelper.GetTaskInnerTypeOrSelf(type); HttpRequest request = context.HttpContext.Request; if (request == null) { throw Error.InvalidOperation(SRResources.WriteToStreamAsyncMustHaveRequest); } HttpResponse response = context.HttpContext.Response; response.ContentType = context.ContentType.Value; MediaTypeHeaderValue contentType = GetContentType(response.Headers[HeaderNames.ContentType].FirstOrDefault()); // Determine the content type. MediaTypeHeaderValue newMediaType = null; if (ODataOutputFormatterHelper.TryGetContentHeader(type, contentType, out newMediaType)) { response.Headers[HeaderNames.ContentType] = new StringValues(newMediaType.ToString()); } // Set the character set. MediaTypeHeaderValue currentContentType = GetContentType(response.Headers[HeaderNames.ContentType].FirstOrDefault()); RequestHeaders requestHeader = request.GetTypedHeaders(); if (requestHeader != null && requestHeader.AcceptCharset != null) { IEnumerable <string> acceptCharsetValues = requestHeader.AcceptCharset.Select(cs => cs.Value.Value); string newCharSet = string.Empty; if (ODataOutputFormatterHelper.TryGetCharSet(currentContentType, acceptCharsetValues, out newCharSet)) { currentContentType.CharSet = newCharSet; response.Headers[HeaderNames.ContentType] = new StringValues(currentContentType.ToString()); } } // Add version header. response.Headers[ODataVersionConstraint.ODataServiceVersionHeader] = ODataUtils.ODataVersionToString(ResultHelpers.GetODataResponseVersion(request)); }
/// <inheritdoc/> public override void SetDefaultContentHeaders(Type type, HttpContentHeaders headers, MediaTypeHeaderValue mediaType) { // Determine the content type or let base class handle it. MediaTypeHeaderValue newMediaType = null; if (ODataOutputFormatterHelper.TryGetContentHeader(type, mediaType, out newMediaType)) { headers.ContentType = newMediaType; } else { // This is the case when a user creates a new ObjectContent<T> passing in a null mediaType base.SetDefaultContentHeaders(type, headers, mediaType); } // Set the character set. IEnumerable <string> acceptCharsetValues = Request.Headers.AcceptCharset.Select(cs => cs.Value); string newCharSet = String.Empty; if (ODataOutputFormatterHelper.TryGetCharSet(headers.ContentType, acceptCharsetValues, out newCharSet)) { headers.ContentType.CharSet = newCharSet; } // Add version header. headers.TryAddWithoutValidation( ODataVersionConstraint.ODataServiceVersionHeader, ODataUtils.ODataVersionToString(ResultHelpers.GetODataResponseVersion(Request))); // Add Preference-Applied headers var responseMessage = ODataOutputFormatterHelper.PrepareResponseMessage( new WebApiRequestMessage(Request), new WebApiRequestHeaders(Request.Headers), (services) => ODataMessageWrapperHelper.Create(null, headers, services)); foreach (var header in responseMessage.Headers) { if (!headers.Contains(header.Key)) { headers.TryAddWithoutValidation(header.Key, header.Value); } } }