forked from aspnet/AspNetWebStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInvalidModelStateResult.cs
More file actions
105 lines (91 loc) · 4.44 KB
/
Copy pathInvalidModelStateResult.cs
File metadata and controls
105 lines (91 loc) · 4.44 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
// 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;
using System.Web.Http.ModelBinding;
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"/> based on a <see cref="ModelStateDictionary"/>.
/// </summary>
public class InvalidModelStateResult : IHttpActionResult
{
private readonly ModelStateDictionary _modelState;
private readonly ExceptionResult.IDependencyProvider _dependencies;
/// <summary>Initializes a new instance of the <see cref="InvalidModelStateResult"/> class.</summary>
/// <param name="modelState">The model state to include in the error.</param>
/// <param name="includeErrorDetail">
/// <see langword="true"/> if the error should include exception messages; otherwise, <see langword="false"/>.
/// </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 InvalidModelStateResult(ModelStateDictionary modelState, bool includeErrorDetail,
IContentNegotiator contentNegotiator, HttpRequestMessage request,
IEnumerable<MediaTypeFormatter> formatters)
: this(modelState, new ExceptionResult.DirectDependencyProvider(includeErrorDetail, contentNegotiator,
request, formatters))
{
}
/// <summary>Initializes a new instance of the <see cref="InvalidModelStateResult"/> class.</summary>
/// <param name="modelState">The model state to include in the error.</param>
/// <param name="controller">The controller from which to obtain the dependencies needed for execution.</param>
public InvalidModelStateResult(ModelStateDictionary modelState, ApiController controller)
: this(modelState, new ExceptionResult.ApiControllerDependencyProvider(controller))
{
}
private InvalidModelStateResult(ModelStateDictionary modelState,
ExceptionResult.IDependencyProvider dependencies)
{
if (modelState == null)
{
throw new ArgumentNullException("modelState");
}
Contract.Assert(dependencies != null);
_modelState = modelState;
_dependencies = dependencies;
}
/// <summary>Gets the model state to include in the error.</summary>
public ModelStateDictionary ModelState
{
get { return _modelState; }
}
/// <summary>Gets a value indicating whether the error should include exception messages.</summary>
public bool IncludeErrorDetail
{
get { return _dependencies.IncludeErrorDetail; }
}
/// <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(_modelState, _dependencies.IncludeErrorDetail);
return NegotiatedContentResult<HttpError>.Execute(HttpStatusCode.BadRequest, error,
_dependencies.ContentNegotiator, _dependencies.Request, _dependencies.Formatters);
}
}
}