forked from aspnet/AspNetWebStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHttpConfigurationExtensions.cs
More file actions
196 lines (176 loc) · 8.55 KB
/
HttpConfigurationExtensions.cs
File metadata and controls
196 lines (176 loc) · 8.55 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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
// 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.ComponentModel;
using System.Diagnostics.CodeAnalysis;
using System.Diagnostics.Contracts;
using System.Web.Http.Controllers;
using System.Web.Http.Filters;
using System.Web.Http.Hosting;
using System.Web.Http.ModelBinding;
using System.Web.Http.ModelBinding.Binders;
using System.Web.Http.Routing;
namespace System.Web.Http
{
[EditorBrowsable(EditorBrowsableState.Never)]
public static class HttpConfigurationExtensions
{
/// <summary>
/// Register that the given parameter type on an Action is to be bound using the model binder.
/// </summary>
/// <param name="configuration">configuration to be updated.</param>
/// <param name="type">parameter type that binder is applied to</param>
/// <param name="binder">a model binder</param>
public static void BindParameter(this HttpConfiguration configuration, Type type, IModelBinder binder)
{
if (configuration == null)
{
throw Error.ArgumentNull("configuration");
}
if (type == null)
{
throw Error.ArgumentNull("type");
}
if (binder == null)
{
throw Error.ArgumentNull("binder");
}
// Add a provider so that we can use this type recursively
// Be sure to insert at position 0 to preempt any eager binders (eg, MutableObjectBinder) that
// may eagerly claim all types.
configuration.Services.Insert(typeof(ModelBinderProvider), 0, new SimpleModelBinderProvider(type, binder));
// Add the binder to the list of rules.
// This ensures that the parameter binding will actually use model binding instead of Formatters.
// Without this, the parameter binding system may see the parameter type is complex and choose
// to use formatters instead, in which case it would ignore the registered model binders.
configuration.ParameterBindingRules.Insert(0, type, param => param.BindWithModelBinding(binder));
}
/// <summary>
/// Maps the attribute-defined routes for the application.
/// </summary>
/// <param name="configuration">The server configuration.</param>
// Corresponds to the MVC implementation of attribute routing in
// System.Web.Mvc.RouteCollectionAttributeRoutingExtensions.
public static void MapHttpAttributeRoutes(this HttpConfiguration configuration)
{
if (configuration == null)
{
throw new ArgumentNullException("configuration");
}
AttributeRoutingMapper.MapAttributeRoutes(configuration, new DefaultInlineConstraintResolver(), new DefaultDirectRouteProvider());
}
/// <summary>
/// Maps the attribute-defined routes for the application.
/// </summary>
/// <param name="configuration">The server configuration.</param>
/// <param name="constraintResolver">
/// The <see cref="IInlineConstraintResolver"/> to use for resolving inline constraints.
/// </param>
// Corresponds to the MVC implementation of attribute routing in
// System.Web.Mvc.RouteCollectionAttributeRoutingExtensions.
public static void MapHttpAttributeRoutes(this HttpConfiguration configuration,
IInlineConstraintResolver constraintResolver)
{
if (configuration == null)
{
throw new ArgumentNullException("configuration");
}
if (constraintResolver == null)
{
throw new ArgumentNullException("constraintResolver");
}
AttributeRoutingMapper.MapAttributeRoutes(configuration, constraintResolver, new DefaultDirectRouteProvider());
}
/// <summary>
/// Maps the attribute-defined routes for the application.
/// </summary>
/// <param name="configuration">The server configuration.</param>
/// <param name="directRouteProvider">
/// The <see cref="IDirectRouteProvider"/> to use for discovering and building routes.
/// </param>
// Corresponds to the MVC implementation of attribute routing in
// System.Web.Mvc.RouteCollectionAttributeRoutingExtensions.
public static void MapHttpAttributeRoutes(
this HttpConfiguration configuration,
IDirectRouteProvider directRouteProvider)
{
if (configuration == null)
{
throw new ArgumentNullException("configuration");
}
if (directRouteProvider == null)
{
throw new ArgumentNullException("directRouteProvider");
}
AttributeRoutingMapper.MapAttributeRoutes(configuration, new DefaultInlineConstraintResolver(), directRouteProvider);
}
/// <summary>
/// Maps the attribute-defined routes for the application.
/// </summary>
/// <param name="configuration">The server configuration.</param>
/// <param name="constraintResolver">
/// The <see cref="IInlineConstraintResolver"/> to use for resolving inline constraints.
/// </param>
/// <param name="directRouteProvider">
/// The <see cref="IDirectRouteProvider"/> to use for discovering and building routes.
/// </param>
// Corresponds to the MVC implementation of attribute routing in
// System.Web.Mvc.RouteCollectionAttributeRoutingExtensions.
public static void MapHttpAttributeRoutes(
this HttpConfiguration configuration,
IInlineConstraintResolver constraintResolver,
IDirectRouteProvider directRouteProvider)
{
if (configuration == null)
{
throw new ArgumentNullException("configuration");
}
if (constraintResolver == null)
{
throw new ArgumentNullException("constraintResolver");
}
if (directRouteProvider == null)
{
throw new ArgumentNullException("directRouteProvider");
}
AttributeRoutingMapper.MapAttributeRoutes(configuration, constraintResolver, directRouteProvider);
}
// Test Hook for inspecting the route table generated by MapHttpAttributeRoutes.
// MapHttpAttributeRoutes doesn't return the route collection because it's an implementation detail
// that attr routes even generate a meaningful route collection.
// Public APIs can get similar functionality by querying the IHttpRoute for IReadOnlyCollection<IHttpRoute>.
internal static IReadOnlyCollection<IHttpRoute> GetAttributeRoutes(this HttpConfiguration configuration)
{
configuration.EnsureInitialized();
HttpRouteCollection routes = configuration.Routes;
foreach (IHttpRoute route in routes)
{
var attrRoute = route as IReadOnlyCollection<IHttpRoute>;
if (attrRoute != null)
{
return attrRoute;
}
}
return null;
}
/// <summary>Enables suppression of the host's principal.</summary>
/// <param name="configuration">The server configuration.</param>
/// <remarks>
/// When the host's principal is suppressed, the current principal is set to anonymous upon entering the
/// <see cref="HttpServer"/>'s first message handler. As a result, any authentication performed by the host is
/// ignored. The remaining pipeline within the <see cref="HttpServer"/>, including
/// <see cref="IAuthenticationFilter"/>s, is then the exclusive authority for authentication.
/// </remarks>
[SuppressMessage("Microsoft.Reliability", "CA2000:Dispose objects before losing scope",
Justification = "Message handler should be disposed with parent configuration.")]
public static void SuppressHostPrincipal(this HttpConfiguration configuration)
{
if (configuration == null)
{
throw new ArgumentNullException("configuration");
}
Contract.Assert(configuration.MessageHandlers != null);
configuration.MessageHandlers.Insert(0, new SuppressHostPrincipalMessageHandler());
}
}
}