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

Commit fb0f76b

Browse files
committed
Fix for #1277 - Add Options/Startup API for WebAPI shim
Adds an options class, as well as a default options setup that will configure the default set of formatters. Currently most of what options needs to do is a placeholder, but it later do things like add ApplicationModelConventions, filters, formatters, model binders, etc. Those will be added in follow up items.
1 parent bb8ba6e commit fb0f76b

File tree

6 files changed

+107
-6
lines changed

6 files changed

+107
-6
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved.
2+
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
3+
4+
using System.Linq;
5+
using System.Net.Http.Formatting;
6+
7+
namespace Microsoft.AspNet.Mvc.WebApiCompatShim
8+
{
9+
public class WebApiCompatShimOptions
10+
{
11+
public WebApiCompatShimOptions()
12+
{
13+
// Start with an empty collection, our options setup will add the default formatters.
14+
Formatters = new MediaTypeFormatterCollection(Enumerable.Empty<MediaTypeFormatter>());
15+
}
16+
17+
public MediaTypeFormatterCollection Formatters { get; set; }
18+
}
19+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved.
2+
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
3+
4+
using System.Net.Http.Formatting;
5+
using Microsoft.Framework.OptionsModel;
6+
7+
namespace Microsoft.AspNet.Mvc.WebApiCompatShim
8+
{
9+
public class WebApiCompatShimOptionsSetup : IOptionsAction<MvcOptions>, IOptionsAction<WebApiCompatShimOptions>
10+
{
11+
public int Order
12+
{
13+
// We want to run after the default MvcOptionsSetup.
14+
get { return DefaultOrder.DefaultFrameworkSortOrder + 10; }
15+
}
16+
17+
public string Name { get; set; }
18+
19+
public void Invoke(MvcOptions options)
20+
{
21+
// Placeholder
22+
}
23+
24+
public void Invoke(WebApiCompatShimOptions options)
25+
{
26+
// Add the default formatters
27+
options.Formatters.AddRange(new MediaTypeFormatterCollection());
28+
}
29+
}
30+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved.
2+
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
3+
4+
using Microsoft.AspNet.Mvc.WebApiCompatShim;
5+
6+
namespace Microsoft.Framework.DependencyInjection
7+
{
8+
public static class WebApiCompatShimServiceCollectionExtensions
9+
{
10+
public static IServiceCollection AddWebApiConventions(this IServiceCollection services)
11+
{
12+
services.AddOptionsAction<WebApiCompatShimOptionsSetup>();
13+
return services;
14+
}
15+
}
16+
}

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

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
using Microsoft.AspNet.TestHost;
99
using Xunit;
1010
using System.Net;
11+
using Newtonsoft.Json;
12+
using System.Net.Http.Formatting;
1113

1214
namespace Microsoft.AspNet.Mvc.FunctionalTests
1315
{
@@ -23,8 +25,6 @@ public async Task ApiController_Activates_HttpContextAndUser()
2325
var server = TestServer.Create(_provider, _app);
2426
var client = server.CreateClient();
2527

26-
27-
2828
// Act
2929
var response = await client.GetAsync("http://localhost/BasicApi/WriteToHttpContext");
3030
var content = await response.Content.ReadAsStringAsync();
@@ -43,8 +43,6 @@ public async Task ApiController_Activates_UrlHelper()
4343
var server = TestServer.Create(_provider, _app);
4444
var client = server.CreateClient();
4545

46-
47-
4846
// Act
4947
var response = await client.GetAsync("http://localhost/BasicApi/GenerateUrl");
5048
var content = await response.Content.ReadAsStringAsync();
@@ -55,6 +53,31 @@ public async Task ApiController_Activates_UrlHelper()
5553
"Visited: /BasicApi/GenerateUrl",
5654
content);
5755
}
56+
57+
[Fact]
58+
public async Task Options_SetsDefaultFormatters()
59+
{
60+
// Arrange
61+
var server = TestServer.Create(_provider, _app);
62+
var client = server.CreateClient();
63+
64+
var expected = new string[]
65+
{
66+
typeof(JsonMediaTypeFormatter).FullName,
67+
typeof(XmlMediaTypeFormatter).FullName,
68+
typeof(FormUrlEncodedMediaTypeFormatter).FullName,
69+
};
70+
71+
// Act
72+
var response = await client.GetAsync("http://localhost/BasicApi/GetFormatters");
73+
var content = await response.Content.ReadAsStringAsync();
74+
75+
var formatters = JsonConvert.DeserializeObject<string[]>(content);
76+
77+
// Assert
78+
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
79+
Assert.Equal(expected, formatters);
80+
}
5881
}
5982
}
6083
#endif

test/WebSites/WebApiCompatShimWebSite/Controllers/BasicApiController.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,21 @@
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.Linq;
45
using System.Threading.Tasks;
56
using System.Web.Http;
67
using Microsoft.AspNet.Http;
78
using Microsoft.AspNet.Mvc;
9+
using Microsoft.AspNet.Mvc.WebApiCompatShim;
10+
using Microsoft.Framework.OptionsModel;
811

912
namespace WebApiCompatShimWebSite
1013
{
1114
public class BasicApiController : ApiController
1215
{
16+
[Activate]
17+
public IOptionsAccessor<WebApiCompatShimOptions> OptionsAccessor { get; set; }
18+
1319
// Verifies property activation
1420
[HttpGet]
1521
public async Task<IActionResult> WriteToHttpContext()
@@ -32,5 +38,12 @@ public async Task<IActionResult> GenerateUrl()
3238
await Context.Response.WriteAsync(message);
3339
return new EmptyResult();
3440
}
41+
42+
// Verifies the default options configure formatters correctly.
43+
[HttpGet]
44+
public string[] GetFormatters()
45+
{
46+
return OptionsAccessor.Options.Formatters.Select(f => f.GetType().FullName).ToArray();
47+
}
3548
}
3649
}

test/WebSites/WebApiCompatShimWebSite/Startup.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@ public void Configure(IApplicationBuilder app)
1515
app.UseServices(services =>
1616
{
1717
services.AddMvc(configuration);
18-
});
1918

20-
app.UseMvc();
19+
services.AddWebApiConventions();
20+
});
2121
}
2222
}
2323
}

0 commit comments

Comments
 (0)