This repository was archived by the owner on Dec 14, 2018. It is now read-only.
File tree 6 files changed +109
-2
lines changed
src/Microsoft.AspNet.Mvc.WebApiCompatShim
Microsoft.AspNet.Mvc.FunctionalTests
WebSites/WebApiCompatShimWebSite
6 files changed +109
-2
lines changed Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
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 + 100 ; }
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
+ }
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change 3
3
4
4
#if ASPNET50
5
5
using System ;
6
+ using System . Net ;
7
+ using System . Net . Http . Formatting ;
6
8
using System . Threading . Tasks ;
7
9
using Microsoft . AspNet . Builder ;
8
10
using Microsoft . AspNet . TestHost ;
11
+ using Newtonsoft . Json ;
9
12
using Xunit ;
10
- using System . Net ;
11
13
12
14
namespace Microsoft . AspNet . Mvc . FunctionalTests
13
15
{
@@ -51,6 +53,31 @@ public async Task ApiController_Activates_UrlHelper()
51
53
"Visited: /BasicApi/GenerateUrl" ,
52
54
content ) ;
53
55
}
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
+ }
54
81
}
55
82
}
56
83
#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 . Linq ;
4
5
using System . Threading . Tasks ;
5
6
using System . Web . Http ;
6
7
using Microsoft . AspNet . Http ;
7
8
using Microsoft . AspNet . Mvc ;
9
+ using Microsoft . AspNet . Mvc . WebApiCompatShim ;
10
+ using Microsoft . Framework . OptionsModel ;
8
11
9
12
namespace WebApiCompatShimWebSite
10
13
{
11
14
public class BasicApiController : ApiController
12
15
{
16
+ [ Activate ]
17
+ public IOptionsAccessor < WebApiCompatShimOptions > OptionsAccessor { get ; set ; }
18
+
13
19
// Verifies property activation
14
20
[ HttpGet ]
15
21
public async Task < IActionResult > WriteToHttpContext ( )
@@ -32,5 +38,12 @@ public async Task<IActionResult> GenerateUrl()
32
38
await Context . Response . WriteAsync ( message ) ;
33
39
return new EmptyResult ( ) ;
34
40
}
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
+ }
35
48
}
36
49
}
Original file line number Diff line number Diff line change @@ -15,8 +15,10 @@ public void Configure(IApplicationBuilder app)
15
15
app . UsePerRequestServices ( services =>
16
16
{
17
17
services . AddMvc ( configuration ) ;
18
- } ) ;
19
18
19
+ services . AddWebApiConventions ( ) ;
20
+ } ) ;
21
+
20
22
app . UseMvc ( ) ;
21
23
}
22
24
}
You can’t perform that action at this time.
0 commit comments