forked from aspnet/AspNetWebStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBadRequestErrorMessageResult.cs
More file actions
94 lines (81 loc) · 3.87 KB
/
Copy pathBadRequestErrorMessageResult.cs
File metadata and controls
94 lines (81 loc) · 3.87 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
// 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.Collections.Generic;
using System.Diagnostics.Contracts;
using System.Net;
using System.Net.Http;
using System.Net.Http.Formatting;
using System.Threading;
using System.Threading.Tasks;
namespace System.Web.Http.Results
{
/// <summary>
/// Represents an action result that returns a <see cref="HttpStatusCode.BadRequest"/> response and performs
/// content negotiation on an <see cref="HttpError"/> with a <see cref="HttpError.Message"/>.
/// </summary>
public class BadRequestErrorMessageResult : IHttpActionResult
{
private readonly string _message;
private readonly NegotiatedContentResult<HttpError>.IDependencyProvider _dependencies;
/// <summary>Initializes a new instance of the <see cref="BadRequestErrorMessageResult"/> class.</summary>
/// <param name="message">The user-visible error message.</param>
/// <param name="contentNegotiator">The content negotiator to handle content negotiation.</param>
/// <param name="request">The request message which led to this result.</param>
/// <param name="formatters">The formatters to use to negotiate and format the content.</param>
public BadRequestErrorMessageResult(string message, IContentNegotiator contentNegotiator,
HttpRequestMessage request, IEnumerable<MediaTypeFormatter> formatters)
: this(message, new NegotiatedContentResult<HttpError>.DirectDependencyProvider(contentNegotiator, request,
formatters))
{
}
/// <summary>Initializes a new instance of the <see cref="BadRequestErrorMessageResult"/> class.</summary>
/// <param name="message">The user-visible error message.</param>
/// <param name="controller">The controller from which to obtain the dependencies needed for execution.</param>
public BadRequestErrorMessageResult(string message, ApiController controller)
: this(message, new NegotiatedContentResult<HttpError>.ApiControllerDependencyProvider(controller))
{
}
private BadRequestErrorMessageResult(string message,
NegotiatedContentResult<HttpError>.IDependencyProvider dependencies)
{
if (message == null)
{
throw new ArgumentNullException("message");
}
Contract.Assert(dependencies != null);
_message = message;
_dependencies = dependencies;
}
/// <summary>Gets the user-visible error message.</summary>
public string Message
{
get { return _message; }
}
/// <summary>Gets the content negotiator to handle content negotiation.</summary>
public IContentNegotiator ContentNegotiator
{
get { return _dependencies.ContentNegotiator; }
}
/// <summary>Gets the request message which led to this result.</summary>
public HttpRequestMessage Request
{
get { return _dependencies.Request; }
}
/// <summary>Gets the formatters to use to negotiate and format the content.</summary>
public IEnumerable<MediaTypeFormatter> Formatters
{
get { return _dependencies.Formatters; }
}
/// <inheritdoc />
public virtual Task<HttpResponseMessage> ExecuteAsync(CancellationToken cancellationToken)
{
return Task.FromResult(Execute());
}
private HttpResponseMessage Execute()
{
HttpError error = new HttpError(_message);
return NegotiatedContentResult<HttpError>.Execute(HttpStatusCode.BadRequest, error,
_dependencies.ContentNegotiator, _dependencies.Request, _dependencies.Formatters);
}
}
}