Skip to content
This repository was archived by the owner on Dec 14, 2018. It is now read-only.

Commit 4227215

Browse files
committed
Fix for issue #1281 - Add ModelBinder for HttpRequestMessage
This change adds a ModelBinder that can bind an HttpRequestMessage to an action parameter. This builds on an earlier change to construct and store the request message in the HttpContext via an http feature.
1 parent 65a5191 commit 4227215

File tree

4 files changed

+67
-3
lines changed

4 files changed

+67
-3
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
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+
}

src/Microsoft.AspNet.Mvc.WebApiCompatShim/WebApiCompatShimOptionsSetup.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ public void Invoke(MvcOptions options)
2121
// Add webapi behaviors to controllers with the appropriate attributes
2222
options.ApplicationModelConventions.Add(new WebApiActionConventionsGlobalModelConvention());
2323
options.ApplicationModelConventions.Add(new WebApiOverloadingGlobalModelConvention());
24+
25+
// Add a model binder to be able to bind HttpRequestMessage
26+
options.ModelBinders.Insert(0, new HttpRequestMessageModelBinder());
2427
}
2528

2629
public void Invoke(WebApiCompatShimOptions options)

test/Microsoft.AspNet.Mvc.FunctionalTests/WebApiCompatShimBasicTest.cs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,28 @@ public async Task ApiController_RequestProperty()
101101
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
102102
Assert.Equal(expected, content);
103103
}
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+
}
104126
}
105127
}
106128
#endif

test/WebSites/WebApiCompatShimWebSite/Controllers/HttpRequestMessage/HttpRequestMessageController.cs

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved.
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

4+
using System;
5+
using System.Net.Http;
46
using System.Threading.Tasks;
57
using System.Web.Http;
68
using Microsoft.AspNet.Http;
@@ -12,18 +14,32 @@ public class HttpRequestMessageController : ApiController
1214
{
1315
public async Task<IActionResult> EchoProperty()
1416
{
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+
}
1631

32+
private async Task Echo(HttpRequestMessage request)
33+
{
1734
var message = string.Format(
1835
"{0} {1} {2} {3} {4}",
19-
request.Method,
36+
request.Method,
2037
request.RequestUri.AbsoluteUri,
2138
request.Headers.Host,
2239
request.Content.Headers.ContentLength,
2340
await request.Content.ReadAsStringAsync());
2441

2542
await Context.Response.WriteAsync(message);
26-
return new EmptyResult();
2743
}
2844
}
2945
}

0 commit comments

Comments
 (0)