// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System.Diagnostics.Contracts;
using System.Net;
using System.Net.Http;
using System.Net.Http.Formatting;
using System.Net.Http.Headers;
using System.Threading;
using System.Threading.Tasks;
namespace System.Web.Http.Results
{
/// Represents an action result that returns formatted content.
/// The type of content in the entity body.
public class FormattedContentResult : IHttpActionResult
{
private readonly HttpStatusCode _statusCode;
private readonly T _content;
private readonly MediaTypeFormatter _formatter;
private readonly MediaTypeHeaderValue _mediaType;
private readonly StatusCodeResult.IDependencyProvider _dependencies;
///
/// Initializes a new instance of the class with the values provided.
///
/// The HTTP status code for the response message.
/// The content value to format in the entity body.
/// The formatter to use to format the content.
///
/// The value for the Content-Type header, or to have the formatter pick a default
/// value.
///
/// The request message which led to this result.
public FormattedContentResult(HttpStatusCode statusCode, T content, MediaTypeFormatter formatter,
MediaTypeHeaderValue mediaType, HttpRequestMessage request)
: this(statusCode, content, formatter, mediaType, new StatusCodeResult.DirectDependencyProvider(request))
{
}
///
/// Initializes a new instance of the class with the values provided.
///
/// The HTTP status code for the response message.
/// The content value to format in the entity body.
/// The formatter to use to format the content.
///
/// The value for the Content-Type header, or to have the formatter pick a default
/// value.
///
/// The controller from which to obtain the dependencies needed for execution.
public FormattedContentResult(HttpStatusCode statusCode, T content, MediaTypeFormatter formatter,
MediaTypeHeaderValue mediaType, ApiController controller)
: this(statusCode, content, formatter, mediaType, new StatusCodeResult.ApiControllerDependencyProvider(
controller))
{
}
private FormattedContentResult(HttpStatusCode statusCode, T content, MediaTypeFormatter formatter,
MediaTypeHeaderValue mediaType, StatusCodeResult.IDependencyProvider dependencies)
{
if (formatter == null)
{
throw new ArgumentNullException("formatter");
}
Contract.Assert(dependencies != null);
_statusCode = statusCode;
_content = content;
_formatter = formatter;
_mediaType = mediaType;
_dependencies = dependencies;
}
/// Gets the HTTP status code for the response message.
public HttpStatusCode StatusCode
{
get { return _statusCode; }
}
/// Gets the content value to format in the entity body.
public T Content
{
get { return _content; }
}
/// Gets the formatter to use to format the content.
public MediaTypeFormatter Formatter
{
get { return _formatter; }
}
///
/// Gets the value for the Content-Type header, or to have the formatter pick a default
/// value.
///
public MediaTypeHeaderValue MediaType
{
get { return _mediaType; }
}
/// Gets the request message which led to this result.
public HttpRequestMessage Request
{
get { return _dependencies.Request; }
}
///
public virtual Task ExecuteAsync(CancellationToken cancellationToken)
{
return Task.FromResult(Execute());
}
private HttpResponseMessage Execute()
{
return Execute(_statusCode, _content, _formatter, _mediaType, _dependencies.Request);
}
internal static HttpResponseMessage Execute(HttpStatusCode statusCode, T content, MediaTypeFormatter formatter,
MediaTypeHeaderValue mediaType, HttpRequestMessage request)
{
HttpResponseMessage response = new HttpResponseMessage(statusCode);
try
{
response.Content = new ObjectContent(content, formatter, mediaType);
response.RequestMessage = request;
}
catch
{
response.Dispose();
throw;
}
return response;
}
}
}