forked from aspnet/AspNetWebStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFormattedContentResult.cs
More file actions
139 lines (122 loc) · 5.49 KB
/
Copy pathFormattedContentResult.cs
File metadata and controls
139 lines (122 loc) · 5.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
// 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
{
/// <summary>Represents an action result that returns formatted content.</summary>
/// <typeparam name="T">The type of content in the entity body.</typeparam>
public class FormattedContentResult<T> : IHttpActionResult
{
private readonly HttpStatusCode _statusCode;
private readonly T _content;
private readonly MediaTypeFormatter _formatter;
private readonly MediaTypeHeaderValue _mediaType;
private readonly StatusCodeResult.IDependencyProvider _dependencies;
/// <summary>
/// Initializes a new instance of the <see cref="FormattedContentResult{T}"/> class with the values provided.
/// </summary>
/// <param name="statusCode">The HTTP status code for the response message.</param>
/// <param name="content">The content value to format in the entity body.</param>
/// <param name="formatter">The formatter to use to format the content.</param>
/// <param name="mediaType">
/// The value for the Content-Type header, or <see langword="null"/> to have the formatter pick a default
/// value.
/// </param>
/// <param name="request">The request message which led to this result.</param>
public FormattedContentResult(HttpStatusCode statusCode, T content, MediaTypeFormatter formatter,
MediaTypeHeaderValue mediaType, HttpRequestMessage request)
: this(statusCode, content, formatter, mediaType, new StatusCodeResult.DirectDependencyProvider(request))
{
}
/// <summary>
/// Initializes a new instance of the <see cref="FormattedContentResult{T}"/> class with the values provided.
/// </summary>
/// <param name="statusCode">The HTTP status code for the response message.</param>
/// <param name="content">The content value to format in the entity body.</param>
/// <param name="formatter">The formatter to use to format the content.</param>
/// <param name="mediaType">
/// The value for the Content-Type header, or <see langword="null"/> to have the formatter pick a default
/// value.
/// </param>
/// <param name="controller">The controller from which to obtain the dependencies needed for execution.</param>
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;
}
/// <summary>Gets the HTTP status code for the response message.</summary>
public HttpStatusCode StatusCode
{
get { return _statusCode; }
}
/// <summary>Gets the content value to format in the entity body.</summary>
public T Content
{
get { return _content; }
}
/// <summary>Gets the formatter to use to format the content.</summary>
public MediaTypeFormatter Formatter
{
get { return _formatter; }
}
/// <summary>
/// Gets the value for the Content-Type header, or <see langword="null"/> to have the formatter pick a default
/// value.
/// </summary>
public MediaTypeHeaderValue MediaType
{
get { return _mediaType; }
}
/// <summary>Gets the request message which led to this result.</summary>
public HttpRequestMessage Request
{
get { return _dependencies.Request; }
}
/// <inheritdoc />
public virtual Task<HttpResponseMessage> 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<T>(content, formatter, mediaType);
response.RequestMessage = request;
}
catch
{
response.Dispose();
throw;
}
return response;
}
}
}