11namespace Microsoft . Examples . V3
22{
3+ using Microsoft . AspNet . OData ;
34 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 ;
68 using System ;
79 using System . Collections . Generic ;
810 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 ;
1114
1215 /// <summary>
1316 /// Represents a RESTful service of products.
@@ -23,8 +26,7 @@ public class ProductsController : ODataController
2326 /// <returns>All available products.</returns>
2427 /// <response code="200">Products successfully retrieved.</response>
2528 [ EnableQuery ]
26- [ Produces ( "application/json" ) ]
27- [ ProducesResponseType ( typeof ( ODataValue < IEnumerable < Product > > ) , Status200OK ) ]
29+ [ ResponseType ( typeof ( ODataValue < IEnumerable < Product > > ) ) ]
2830 public IQueryable < Product > Get ( ) => products ;
2931
3032 /// <summary>
@@ -35,24 +37,19 @@ public class ProductsController : ODataController
3537 /// <response code="200">The product was successfully retrieved.</response>
3638 /// <response code="404">The product does not exist.</response>
3739 [ EnableQuery ]
38- [ Produces ( "application/json" ) ]
39- [ ProducesResponseType ( typeof ( Product ) , Status200OK ) ]
40- [ ProducesResponseType ( Status404NotFound ) ]
40+ [ ResponseType ( typeof ( Product ) ) ]
4141 public SingleResult < Product > Get ( [ FromODataUri ] int key ) => SingleResult . Create ( products . Where ( p => p . Id == key ) ) ;
4242
4343 /// <summary>
4444 /// Creates a new product.
4545 /// </summary>
46- /// <param name="order ">The product to create.</param>
46+ /// <param name="product ">The product to create.</param>
4747 /// <returns>The created product.</returns>
4848 /// <response code="201">The product was successfully created.</response>
4949 /// <response code="204">The product was successfully created.</response>
5050 /// <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 )
5653 {
5754 if ( ! ModelState . IsValid )
5855 {
@@ -74,12 +71,8 @@ public IActionResult Post( [FromBody] Product product )
7471 /// <response code="204">The product was successfully updated.</response>
7572 /// <response code="400">The product is invalid.</response>
7673 /// <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 )
8376 {
8477 if ( ! ModelState . IsValid )
8578 {
@@ -103,12 +96,8 @@ public IActionResult Patch( [FromODataUri] int key, Delta<Product> delta )
10396 /// <response code="204">The product was successfully updated.</response>
10497 /// <response code="400">The product is invalid.</response>
10598 /// <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 )
112101 {
113102 if ( ! ModelState . IsValid )
114103 {
@@ -124,9 +113,7 @@ public IActionResult Put( [FromODataUri] int key, [FromBody] Product update )
124113 /// <param name="key">The product to delete.</param>
125114 /// <returns>None</returns>
126115 /// <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 ) ;
130117
131118 /// <summary>
132119 /// Gets the supplier associated with the product.
@@ -135,9 +122,7 @@ public IActionResult Put( [FromODataUri] int key, [FromBody] Product update )
135122 /// <returns>The supplier</returns>
136123 /// <returns>The requested supplier.</returns>
137124 [ EnableQuery ]
138- [ Produces ( "application/json" ) ]
139- [ ProducesResponseType ( typeof ( Supplier ) , Status200OK ) ]
140- [ ProducesResponseType ( Status404NotFound ) ]
125+ [ ResponseType ( typeof ( Supplier ) ) ]
141126 public SingleResult < Supplier > GetSupplier ( [ FromODataUri ] int key ) => SingleResult . Create ( products . Where ( p => p . Id == key ) . Select ( p => p . Supplier ) ) ;
142127
143128 /// <summary>
@@ -146,29 +131,18 @@ public IActionResult Put( [FromODataUri] int key, [FromBody] Product update )
146131 /// <param name="key">The product identifier.</param>
147132 /// <param name="navigationProperty">The supplier to link.</param>
148133 /// <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 )
153136 {
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 ;
156140
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 ) ;
159142
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 ) ) ;
170144
171- return Ok ( new Uri ( builder . ToString ( ) ) ) ;
145+ return Ok ( relatedKey ) ;
172146 }
173147
174148 /// <summary>
@@ -179,19 +153,15 @@ public IActionResult GetRefToSupplier( [FromODataUri] int key, string navigation
179153 /// <param name="link">The supplier identifier.</param>
180154 /// <returns>None</returns>
181155 [ 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 ) ;
185157
186158 /// <summary>
187159 /// Unlinks a supplier from a product.
188160 /// </summary>
189161 /// <param name="key">The product identifier.</param>
190162 /// <param name="navigationProperty">The supplier to unlink.</param>
191163 /// <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 ) ;
195165
196166 static Product NewProduct ( int id ) =>
197167 new Product ( )
0 commit comments