Skip to content

Commit 7a4178f

Browse files
Update the sample projects to align with the RTW release (#23)
1 parent 9f5a605 commit 7a4178f

File tree

8 files changed

+38
-29
lines changed

8 files changed

+38
-29
lines changed

samples/aspnetcore/BasicSample/Controllers/HelloWorldController.cs

+13-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
namespace Microsoft.Examples.Controllers
22
{
3+
using AspNetCore.Mvc.Routing;
4+
using AspNetCore.Routing;
5+
using Extensions.DependencyInjection;
36
using Microsoft.AspNetCore.Mvc;
47
using System;
58
using System.Collections.Generic;
@@ -12,6 +15,14 @@ public class HelloWorldController : Controller
1215
{
1316
// GET api/v{version}/helloworld
1417
[HttpGet]
15-
public string Get() => $"Controller = {GetType().Name}\nVersion = {HttpContext.GetRequestedApiVersion()}";
18+
public IActionResult Get() => Ok( new { Controller = GetType().Name, Version = HttpContext.GetRequestedApiVersion().ToString() } );
19+
20+
// GET api/v{version}/helloworld/{id}
21+
[HttpGet( "{id:int}", Name = "GetMessageById" )]
22+
public IActionResult Get( int id ) => Ok( new { Controller = GetType().Name, Id = id, Version = HttpContext.GetRequestedApiVersion().ToString() } );
23+
24+
// POST api/v{version}/helloworld
25+
[HttpPost]
26+
public IActionResult Post() => CreatedAtRoute( "GetMessageById", new { id = 42 }, null );
1627
}
17-
}
28+
}

samples/webapi/BasicODataWebApiSample/BasicODataWebApiSample.csproj

+1
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
<WarningLevel>4</WarningLevel>
3636
<NoWarn>
3737
</NoWarn>
38+
<UseVSHostingProcess>true</UseVSHostingProcess>
3839
</PropertyGroup>
3940
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
4041
<DebugType>pdbonly</DebugType>

samples/webapi/BasicODataWebApiSample/Controllers/OrdersController.cs

+2-3
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
{
33
using Microsoft.Web.Http;
44
using Models;
5-
using System.Threading.Tasks;
65
using System.Web.Http;
76
using System.Web.OData;
87
using System.Web.OData.Query;
@@ -13,13 +12,13 @@
1312
public class OrdersController : ODataController
1413
{
1514
// GET ~/v1/orders
16-
// GET ~/orders?api-version=1.0
15+
// GET ~/api/orders?api-version=1.0
1716
[ODataRoute]
1817
public IHttpActionResult Get( ODataQueryOptions<Order> options ) =>
1918
Ok( new[] { new Order() { Id = 1, Customer = "Bill Mei" } } );
2019

2120
// GET ~/v1/orders(1)
22-
// GET ~/orders(1)?api-version=1.0
21+
// GET ~/api/orders(1)?api-version=1.0
2322
[ODataRoute( "({id})" )]
2423
public IHttpActionResult Get( [FromODataUri] int id, ODataQueryOptions<Order> options ) =>
2524
Ok( new Order() { Id = id, Customer = "Bill Mei" } );

samples/webapi/BasicODataWebApiSample/Controllers/People2Controller.cs

+4-3
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
{
33
using Microsoft.Web.Http;
44
using Models;
5-
using System.Threading.Tasks;
65
using System.Web.Http;
76
using System.Web.OData;
87
using System.Web.OData.Query;
@@ -13,12 +12,14 @@
1312
[ODataRoutePrefix( "People" )]
1413
public class People2Controller : ODataController
1514
{
16-
// GET ~/people?api-version=3.0
15+
// GET ~/v3/people
16+
// GET ~/api/people?api-version=3.0
1717
[ODataRoute]
1818
public IHttpActionResult Get( ODataQueryOptions<Person> options ) =>
1919
Ok( new[] { new Person() { Id = 1, FirstName = "Bill", LastName = "Mei", Email = "[email protected]", Phone = "555-555-5555" } } );
2020

21-
// GET ~/people(1)?api-version=3.0
21+
// GET ~/v3/people(1)
22+
// GET ~/api/people(1)?api-version=3.0
2223
[ODataRoute( "({key})" )]
2324
public IHttpActionResult Get( [FromODataUri] int id, ODataQueryOptions<Person> options ) =>
2425
Ok( new Person() { Id = id, FirstName = "Bill", LastName = "Mei", Email = "[email protected]", Phone = "555-555-5555" } );

samples/webapi/BasicODataWebApiSample/Controllers/PeopleController.cs

+6-4
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
{
33
using Microsoft.Web.Http;
44
using Models;
5-
using System.Threading.Tasks;
65
using System.Web.Http;
76
using System.Web.OData;
87
using System.Web.OData.Query;
@@ -14,18 +13,21 @@
1413
public class PeopleController : ODataController
1514
{
1615
// GET ~/v1/people
17-
// GET ~/people?api-version=[1.0|2.0]
16+
// GET ~/v2/people
17+
// GET ~/api/people?api-version=[1.0|2.0]
1818
[ODataRoute]
1919
public IHttpActionResult Get( ODataQueryOptions<Person> options ) =>
2020
Ok( new[] { new Person() { Id = 1, FirstName = "Bill", LastName = "Mei", Email = "[email protected]", Phone = "555-555-5555" } } );
2121

2222
// GET ~/v1/people(1)
23-
// GET ~/people(1)?api-version=[1.0|2.0]
23+
// GET ~/v2/people(1)
24+
// GET ~/api/people(1)?api-version=[1.0|2.0]
2425
[ODataRoute( "({id})" )]
2526
public IHttpActionResult Get( [FromODataUri] int id, ODataQueryOptions<Person> options ) =>
2627
Ok( new Person() { Id = id, FirstName = "Bill", LastName = "Mei", Email = "[email protected]", Phone = "555-555-5555" } );
2728

28-
// PATCH ~/people(1)?api-version=2.0
29+
// PATCH ~/v2/people(1)
30+
// PATCH ~/api/people(1)?api-version=2.0
2931
[MapToApiVersion( "2.0" )]
3032
[ODataRoute( "({id})" )]
3133
public IHttpActionResult Patch( [FromODataUri] int id, Delta<Person> delta, ODataQueryOptions<Person> options )

samples/webapi/BasicODataWebApiSample/Startup.cs

+1-6
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,7 @@ namespace Microsoft.Examples
44
{
55
using Configuration;
66
using global::Owin;
7-
using Microsoft.OData.Edm;
8-
using Microsoft.Web.Http;
97
using Microsoft.Web.OData.Builder;
10-
using System.Linq;
118
using System.Web.Http;
129
using System.Web.OData.Batch;
1310
using System.Web.OData.Builder;
@@ -36,11 +33,9 @@ public void Configuration( IAppBuilder appBuilder )
3633
};
3734
var models = modelBuilder.GetEdmModels();
3835
var batchHandler = new DefaultODataBatchHandler( httpServer );
39-
var v1 = new ApiVersion( 1, 0 );
40-
var modelV1 = models.Single( m => m.GetAnnotationValue<ApiVersionAnnotation>( m ).ApiVersion == v1 );
4136

4237
configuration.MapVersionedODataRoutes( "odata", "api", models, batchHandler );
43-
configuration.MapVersionedODataRoute( "odata-v1", "v1", modelV1, v1, batchHandler );
38+
configuration.MapVersionedODataRoutes( "odata-bypath", "v{apiVersion}", models, batchHandler );
4439
appBuilder.UseWebApi( httpServer );
4540
}
4641
}
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,22 @@
11
namespace Microsoft.Examples.Controllers
22
{
33
using Microsoft.Web.Http;
4-
using System;
5-
using System.Collections.Generic;
6-
using System.Linq;
7-
using System.Threading.Tasks;
84
using System.Web.Http;
95

106
[ApiVersion( "1.0" )]
11-
[Route( "api/v{version:apiVersion}/helloworld" )]
7+
[RoutePrefix( "api/v{version:apiVersion}/helloworld" )]
128
public class HelloWorldController : ApiController
139
{
1410
// GET api/v{version}/helloworld
11+
[Route]
1512
public IHttpActionResult Get() => Ok( new { controller = GetType().Name, version = Request.GetRequestedApiVersion().ToString() } );
13+
14+
// GET api/v{version}/helloworld/{id}
15+
[Route( "{id:int}", Name = "GetMessageById" )]
16+
public IHttpActionResult Get( int id ) => Ok( new { controller = GetType().Name, id = id, version = Request.GetRequestedApiVersion().ToString() } );
17+
18+
// POST api/v{version}/helloworld
19+
[Route]
20+
public IHttpActionResult Post() => CreatedAtRoute( "GetMessageById", new { id = 42 }, default( object ) );
1621
}
1722
}

samples/webapi/ConventionsODataWebApiSample/Startup.cs

+1-6
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,8 @@ namespace Microsoft.Examples
55
using Configuration;
66
using Controllers;
77
using global::Owin;
8-
using Microsoft.OData.Edm;
9-
using Microsoft.Web.Http;
108
using Microsoft.Web.Http.Versioning.Conventions;
119
using Microsoft.Web.OData.Builder;
12-
using System.Linq;
1310
using System.Web.Http;
1411
using System.Web.OData.Batch;
1512
using System.Web.OData.Builder;
@@ -54,11 +51,9 @@ public void Configuration( IAppBuilder appBuilder )
5451
};
5552
var models = modelBuilder.GetEdmModels();
5653
var batchHandler = new DefaultODataBatchHandler( httpServer );
57-
var v1 = new ApiVersion( 1, 0 );
58-
var modelV1 = models.Single( m => m.GetAnnotationValue<ApiVersionAnnotation>( m ).ApiVersion == v1 );
5954

6055
configuration.MapVersionedODataRoutes( "odata", "api", models, batchHandler );
61-
configuration.MapVersionedODataRoute( "odata-v1", "v1", modelV1, v1, batchHandler );
56+
configuration.MapVersionedODataRoutes( "odata-bypath", "v{apiVersion}", models, batchHandler );
6257
appBuilder.UseWebApi( httpServer );
6358
}
6459
}

0 commit comments

Comments
 (0)