1
1
namespace Microsoft . Examples . V3
2
2
{
3
+ using Microsoft . AspNet . OData ;
3
4
using Microsoft . AspNet . OData . Extensions ;
4
- using Microsoft . AspNet . OData . Simulators . Models ;
5
- using Microsoft . AspNetCore . Mvc ;
5
+ using Microsoft . Examples . Models ;
6
+ using Microsoft . OData . UriParser ;
7
+ using Microsoft . Web . Http ;
6
8
using System ;
7
9
using System . Collections . Generic ;
8
10
using System . Linq ;
9
- using System . Text ;
10
- using static Microsoft . AspNetCore . Http . StatusCodes ;
11
+ using System . Web . Http ;
12
+ using System . Web . Http . Description ;
13
+ using static System . Net . HttpStatusCode ;
11
14
12
15
/// <summary>
13
16
/// Represents a RESTful service of products.
@@ -23,8 +26,7 @@ public class ProductsController : ODataController
23
26
/// <returns>All available products.</returns>
24
27
/// <response code="200">Products successfully retrieved.</response>
25
28
[ EnableQuery ]
26
- [ Produces ( "application/json" ) ]
27
- [ ProducesResponseType ( typeof ( ODataValue < IEnumerable < Product > > ) , Status200OK ) ]
29
+ [ ResponseType ( typeof ( ODataValue < IEnumerable < Product > > ) ) ]
28
30
public IQueryable < Product > Get ( ) => products ;
29
31
30
32
/// <summary>
@@ -35,24 +37,19 @@ public class ProductsController : ODataController
35
37
/// <response code="200">The product was successfully retrieved.</response>
36
38
/// <response code="404">The product does not exist.</response>
37
39
[ EnableQuery ]
38
- [ Produces ( "application/json" ) ]
39
- [ ProducesResponseType ( typeof ( Product ) , Status200OK ) ]
40
- [ ProducesResponseType ( Status404NotFound ) ]
40
+ [ ResponseType ( typeof ( Product ) ) ]
41
41
public SingleResult < Product > Get ( [ FromODataUri ] int key ) => SingleResult . Create ( products . Where ( p => p . Id == key ) ) ;
42
42
43
43
/// <summary>
44
44
/// Creates a new product.
45
45
/// </summary>
46
- /// <param name="order ">The product to create.</param>
46
+ /// <param name="product ">The product to create.</param>
47
47
/// <returns>The created product.</returns>
48
48
/// <response code="201">The product was successfully created.</response>
49
49
/// <response code="204">The product was successfully created.</response>
50
50
/// <response code="400">The product is invalid.</response>
51
- [ Produces ( "application/json" ) ]
52
- [ ProducesResponseType ( typeof ( Product ) , Status201Created ) ]
53
- [ ProducesResponseType ( Status204NoContent ) ]
54
- [ ProducesResponseType ( Status400BadRequest ) ]
55
- public IActionResult Post ( [ FromBody ] Product product )
51
+ [ ResponseType ( typeof ( Product ) ) ]
52
+ public IHttpActionResult Post ( [ FromBody ] Product product )
56
53
{
57
54
if ( ! ModelState . IsValid )
58
55
{
@@ -74,12 +71,8 @@ public IActionResult Post( [FromBody] Product product )
74
71
/// <response code="204">The product was successfully updated.</response>
75
72
/// <response code="400">The product is invalid.</response>
76
73
/// <response code="404">The product does not exist.</response>
77
- [ Produces ( "application/json" ) ]
78
- [ ProducesResponseType ( typeof ( Product ) , Status200OK ) ]
79
- [ ProducesResponseType ( Status204NoContent ) ]
80
- [ ProducesResponseType ( Status400BadRequest ) ]
81
- [ ProducesResponseType ( Status404NotFound ) ]
82
- public IActionResult Patch ( [ FromODataUri ] int key , Delta < Product > delta )
74
+ [ ResponseType ( typeof ( Product ) ) ]
75
+ public IHttpActionResult Patch ( [ FromODataUri ] int key , Delta < Product > delta )
83
76
{
84
77
if ( ! ModelState . IsValid )
85
78
{
@@ -103,12 +96,8 @@ public IActionResult Patch( [FromODataUri] int key, Delta<Product> delta )
103
96
/// <response code="204">The product was successfully updated.</response>
104
97
/// <response code="400">The product is invalid.</response>
105
98
/// <response code="404">The product does not exist.</response>
106
- [ Produces ( "application/json" ) ]
107
- [ ProducesResponseType ( typeof ( Product ) , Status200OK ) ]
108
- [ ProducesResponseType ( Status204NoContent ) ]
109
- [ ProducesResponseType ( Status400BadRequest ) ]
110
- [ ProducesResponseType ( Status404NotFound ) ]
111
- public IActionResult Put ( [ FromODataUri ] int key , [ FromBody ] Product update )
99
+ [ ResponseType ( typeof ( Product ) ) ]
100
+ public IHttpActionResult Put ( [ FromODataUri ] int key , [ FromBody ] Product update )
112
101
{
113
102
if ( ! ModelState . IsValid )
114
103
{
@@ -124,9 +113,7 @@ public IActionResult Put( [FromODataUri] int key, [FromBody] Product update )
124
113
/// <param name="key">The product to delete.</param>
125
114
/// <returns>None</returns>
126
115
/// <response code="204">The product was successfully deleted.</response>
127
- [ ProducesResponseType ( Status204NoContent ) ]
128
- [ ProducesResponseType ( Status404NotFound ) ]
129
- public IActionResult Delete ( [ FromODataUri ] int key ) => NoContent ( ) ;
116
+ public IHttpActionResult Delete ( [ FromODataUri ] int key ) => StatusCode ( NoContent ) ;
130
117
131
118
/// <summary>
132
119
/// Gets the supplier associated with the product.
@@ -135,9 +122,7 @@ public IActionResult Put( [FromODataUri] int key, [FromBody] Product update )
135
122
/// <returns>The supplier</returns>
136
123
/// <returns>The requested supplier.</returns>
137
124
[ EnableQuery ]
138
- [ Produces ( "application/json" ) ]
139
- [ ProducesResponseType ( typeof ( Supplier ) , Status200OK ) ]
140
- [ ProducesResponseType ( Status404NotFound ) ]
125
+ [ ResponseType ( typeof ( Supplier ) ) ]
141
126
public SingleResult < Supplier > GetSupplier ( [ FromODataUri ] int key ) => SingleResult . Create ( products . Where ( p => p . Id == key ) . Select ( p => p . Supplier ) ) ;
142
127
143
128
/// <summary>
@@ -146,29 +131,18 @@ public IActionResult Put( [FromODataUri] int key, [FromBody] Product update )
146
131
/// <param name="key">The product identifier.</param>
147
132
/// <param name="navigationProperty">The supplier to link.</param>
148
133
/// <returns>The supplier link.</returns>
149
- [ Produces ( "application/json" ) ]
150
- [ ProducesResponseType ( typeof ( ODataId ) , Status200OK ) ]
151
- [ ProducesResponseType ( Status404NotFound ) ]
152
- public IActionResult GetRefToSupplier ( [ FromODataUri ] int key , string navigationProperty )
134
+ [ ResponseType ( typeof ( ODataId ) ) ]
135
+ public IHttpActionResult GetRefToSupplier ( [ FromODataUri ] int key , string navigationProperty )
153
136
{
154
- var routeName = Request . ODataFeature ( ) . RouteName ;
155
- var builder = new StringBuilder ( Request . Scheme ) ;
137
+ var segments = Request . ODataProperties ( ) . Path . Segments . ToArray ( ) ;
138
+ var entitySet = ( ( EntitySetSegment ) segments [ 0 ] ) . EntitySet ;
139
+ var property = entitySet . NavigationPropertyBindings . Single ( p => p . Path . Path == navigationProperty ) . NavigationProperty ;
156
140
157
- builder . Append ( Uri . SchemeDelimiter ) ;
158
- builder . Append ( Request . Host . HasValue ? Request . Host . Value : Request . Host . ToString ( ) ) ;
141
+ segments [ segments . Length - 1 ] = new NavigationPropertySegment ( property , entitySet ) ;
159
142
160
- if ( ! string . IsNullOrEmpty ( routeName ) )
161
- {
162
- builder . Append ( '/' ) ;
163
- builder . Append ( routeName ) ;
164
- }
165
-
166
- builder . Append ( "/Products/" ) ;
167
- builder . Append ( key ) ;
168
- builder . Append ( '/' ) ;
169
- builder . Append ( navigationProperty ) ;
143
+ var relatedKey = new Uri ( Url . CreateODataLink ( segments ) ) ;
170
144
171
- return Ok ( new Uri ( builder . ToString ( ) ) ) ;
145
+ return Ok ( relatedKey ) ;
172
146
}
173
147
174
148
/// <summary>
@@ -179,19 +153,15 @@ public IActionResult GetRefToSupplier( [FromODataUri] int key, string navigation
179
153
/// <param name="link">The supplier identifier.</param>
180
154
/// <returns>None</returns>
181
155
[ HttpPut ]
182
- [ ProducesResponseType ( Status204NoContent ) ]
183
- [ ProducesResponseType ( Status404NotFound ) ]
184
- public IActionResult CreateRefToSupplier ( [ FromODataUri ] int key , string navigationProperty , [ FromBody ] Uri link ) => NoContent ( ) ;
156
+ public IHttpActionResult CreateRefToSupplier ( [ FromODataUri ] int key , string navigationProperty , [ FromBody ] Uri link ) => StatusCode ( NoContent ) ;
185
157
186
158
/// <summary>
187
159
/// Unlinks a supplier from a product.
188
160
/// </summary>
189
161
/// <param name="key">The product identifier.</param>
190
162
/// <param name="navigationProperty">The supplier to unlink.</param>
191
163
/// <returns>None</returns>
192
- [ ProducesResponseType ( Status204NoContent ) ]
193
- [ ProducesResponseType ( Status404NotFound ) ]
194
- public IActionResult DeleteRefToSupplier ( [ FromODataUri ] int key , string navigationProperty ) => NoContent ( ) ;
164
+ public IHttpActionResult DeleteRefToSupplier ( [ FromODataUri ] int key , string navigationProperty ) => StatusCode ( NoContent ) ;
195
165
196
166
static Product NewProduct ( int id ) =>
197
167
new Product ( )
0 commit comments