This repository was archived by the owner on Dec 14, 2018. It is now read-only.
File tree 4 files changed +67
-3
lines changed
src/Microsoft.AspNet.Mvc.WebApiCompatShim
Microsoft.AspNet.Mvc.FunctionalTests
WebSites/WebApiCompatShimWebSite/Controllers/HttpRequestMessage 4 files changed +67
-3
lines changed Original file line number Diff line number Diff line change
1
+ using Microsoft . AspNet . Mvc . ModelBinding ;
2
+ // Copyright (c) Microsoft Open Technologies, Inc. All rights reserved.
3
+ // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
4
+
5
+ using System . Net . Http ;
6
+ using System . Threading . Tasks ;
7
+
8
+ namespace Microsoft . AspNet . Mvc . WebApiCompatShim
9
+ {
10
+ public class HttpRequestMessageModelBinder : IModelBinder
11
+ {
12
+ public Task < bool > BindModelAsync ( ModelBindingContext bindingContext )
13
+ {
14
+ if ( bindingContext . ModelType == typeof ( HttpRequestMessage ) )
15
+ {
16
+ bindingContext . Model = bindingContext . HttpContext . GetHttpRequestMessage ( ) ;
17
+ return Task . FromResult ( true ) ;
18
+ }
19
+
20
+ return Task . FromResult ( false ) ;
21
+ }
22
+ }
23
+ }
Original file line number Diff line number Diff line change @@ -21,6 +21,9 @@ public void Invoke(MvcOptions options)
21
21
// Add webapi behaviors to controllers with the appropriate attributes
22
22
options . ApplicationModelConventions . Add ( new WebApiActionConventionsGlobalModelConvention ( ) ) ;
23
23
options . ApplicationModelConventions . Add ( new WebApiOverloadingGlobalModelConvention ( ) ) ;
24
+
25
+ // Add a model binder to be able to bind HttpRequestMessage
26
+ options . ModelBinders . Insert ( 0 , new HttpRequestMessageModelBinder ( ) ) ;
24
27
}
25
28
26
29
public void Invoke ( WebApiCompatShimOptions options )
Original file line number Diff line number Diff line change @@ -101,6 +101,28 @@ public async Task ApiController_RequestProperty()
101
101
Assert . Equal ( HttpStatusCode . OK , response . StatusCode ) ;
102
102
Assert . Equal ( expected , content ) ;
103
103
}
104
+
105
+ [ Fact ]
106
+ public async Task ApiController_RequestParameter ( )
107
+ {
108
+ // Arrange
109
+ var server = TestServer . Create ( _provider , _app ) ;
110
+ var client = server . CreateClient ( ) ;
111
+
112
+ var expected =
113
+ "POST http://localhost/api/Blog/HttpRequestMessage/EchoParameter localhost " +
114
+ "17 Hello, the world!" ;
115
+
116
+ // Act
117
+ var response = await client . PostAsync (
118
+ "http://localhost/api/Blog/HttpRequestMessage/EchoParameter" ,
119
+ new StringContent ( "Hello, the world!" ) ) ;
120
+ var content = await response . Content . ReadAsStringAsync ( ) ;
121
+
122
+ // Assert
123
+ Assert . Equal ( HttpStatusCode . OK , response . StatusCode ) ;
124
+ Assert . Equal ( expected , content ) ;
125
+ }
104
126
}
105
127
}
106
128
#endif
Original file line number Diff line number Diff line change 1
1
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved.
2
2
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
3
3
4
+ using System ;
5
+ using System . Net . Http ;
4
6
using System . Threading . Tasks ;
5
7
using System . Web . Http ;
6
8
using Microsoft . AspNet . Http ;
@@ -12,18 +14,32 @@ public class HttpRequestMessageController : ApiController
12
14
{
13
15
public async Task < IActionResult > EchoProperty ( )
14
16
{
15
- var request = Request ;
17
+ await Echo ( Request ) ;
18
+ return new EmptyResult ( ) ;
19
+ }
20
+
21
+ public async Task < IActionResult > EchoParameter ( HttpRequestMessage request )
22
+ {
23
+ if ( ! object . ReferenceEquals ( request , Request ) )
24
+ {
25
+ throw new InvalidOperationException ( ) ;
26
+ }
27
+
28
+ await Echo ( request ) ;
29
+ return new EmptyResult ( ) ;
30
+ }
16
31
32
+ private async Task Echo ( HttpRequestMessage request )
33
+ {
17
34
var message = string . Format (
18
35
"{0} {1} {2} {3} {4}" ,
19
- request . Method ,
36
+ request . Method ,
20
37
request . RequestUri . AbsoluteUri ,
21
38
request . Headers . Host ,
22
39
request . Content . Headers . ContentLength ,
23
40
await request . Content . ReadAsStringAsync ( ) ) ;
24
41
25
42
await Context . Response . WriteAsync ( message ) ;
26
- return new EmptyResult ( ) ;
27
43
}
28
44
}
29
45
}
You can’t perform that action at this time.
0 commit comments