Skip to content

Commit 51b7148

Browse files
Added support for API versioning conventions (#16)
1 parent c3f9cfb commit 51b7148

File tree

85 files changed

+5184
-273
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

85 files changed

+5184
-273
lines changed

ApiVersioning.sln

+13
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,18 @@ Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "Microsoft.AspNet.WebApi.Ver
8585
EndProject
8686
Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "Microsoft.AspNet.OData.Versioning.Tests", "test\Microsoft.AspNet.OData.Versioning.Tests\Microsoft.AspNet.OData.Versioning.Tests.xproj", "{D87E54CC-C2D6-4AE5-806D-AE825B051C66}"
8787
EndProject
88+
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Conventions", "Conventions", "{B24995FB-AF48-4E5D-9327-377A599BDE2A}"
89+
ProjectSection(SolutionItems) = preProject
90+
src\Common\Versioning\Conventions\ActionApiVersionConventionBuilderT.cs = src\Common\Versioning\Conventions\ActionApiVersionConventionBuilderT.cs
91+
src\Common\Versioning\Conventions\ActionApiVersionConventionBuilderTExtensions.cs = src\Common\Versioning\Conventions\ActionApiVersionConventionBuilderTExtensions.cs
92+
src\Common\Versioning\Conventions\ActionConventionBuilderTExtensions.cs = src\Common\Versioning\Conventions\ActionConventionBuilderTExtensions.cs
93+
src\Common\Versioning\Conventions\ControllerApiVersionConventionBuilderT.cs = src\Common\Versioning\Conventions\ControllerApiVersionConventionBuilderT.cs
94+
src\Common\Versioning\Conventions\ControllerApiVersionConventionBuilderTExtensions.cs = src\Common\Versioning\Conventions\ControllerApiVersionConventionBuilderTExtensions.cs
95+
src\Common\Versioning\Conventions\ExpressionExtensions.cs = src\Common\Versioning\Conventions\ExpressionExtensions.cs
96+
src\Common\Versioning\Conventions\IActionConventionBuilderT.cs = src\Common\Versioning\Conventions\IActionConventionBuilderT.cs
97+
src\Common\Versioning\Conventions\IApiVersionConventionT.cs = src\Common\Versioning\Conventions\IApiVersionConventionT.cs
98+
EndProjectSection
99+
EndProject
88100
Global
89101
GlobalSection(SolutionConfigurationPlatforms) = preSolution
90102
Debug|Any CPU = Debug|Any CPU
@@ -131,5 +143,6 @@ Global
131143
{69C59656-53D1-4ACB-92B5-8B34C8E62175} = {0987757E-4D09-4523-B9C9-65B1E8832AA1}
132144
{AEB074E1-E57A-4DD3-A972-3625B367CE5D} = {0987757E-4D09-4523-B9C9-65B1E8832AA1}
133145
{D87E54CC-C2D6-4AE5-806D-AE825B051C66} = {0987757E-4D09-4523-B9C9-65B1E8832AA1}
146+
{B24995FB-AF48-4E5D-9327-377A599BDE2A} = {DE4EE45F-F8EA-4B32-B16F-441F946ACEF4}
134147
EndGlobalSection
135148
EndGlobal

ApiVersioningWithSamples.sln

+21
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,12 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AdvancedODataWebApiSample",
106106
EndProject
107107
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ByNamespaceWebApiSample", "samples\webapi\ByNamespaceWebApiSample\ByNamespaceWebApiSample.csproj", "{A02A4245-3AEB-4549-9037-D89DFDC7E74D}"
108108
EndProject
109+
Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "ConventionsSamples", "samples\aspnetcore\ConventionsSamples\ConventionsSamples.xproj", "{1EFC221F-35CF-4B55-BD59-240D5B808E14}"
110+
EndProject
111+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ConventionsWebApiSample", "samples\webapi\ConventionsWebApiSample\ConventionsWebApiSample.csproj", "{C1F89961-7134-4D97-BA3A-2693FD1CBF4E}"
112+
EndProject
113+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ConventionsODataWebApiSample", "samples\webapi\ConventionsODataWebApiSample\ConventionsODataWebApiSample.csproj", "{9A22600C-7768-4D16-B67D-514F55942FAF}"
114+
EndProject
109115
Global
110116
GlobalSection(SolutionConfigurationPlatforms) = preSolution
111117
Debug|Any CPU = Debug|Any CPU
@@ -156,6 +162,18 @@ Global
156162
{A02A4245-3AEB-4549-9037-D89DFDC7E74D}.Debug|Any CPU.Build.0 = Debug|Any CPU
157163
{A02A4245-3AEB-4549-9037-D89DFDC7E74D}.Release|Any CPU.ActiveCfg = Release|Any CPU
158164
{A02A4245-3AEB-4549-9037-D89DFDC7E74D}.Release|Any CPU.Build.0 = Release|Any CPU
165+
{1EFC221F-35CF-4B55-BD59-240D5B808E14}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
166+
{1EFC221F-35CF-4B55-BD59-240D5B808E14}.Debug|Any CPU.Build.0 = Debug|Any CPU
167+
{1EFC221F-35CF-4B55-BD59-240D5B808E14}.Release|Any CPU.ActiveCfg = Release|Any CPU
168+
{1EFC221F-35CF-4B55-BD59-240D5B808E14}.Release|Any CPU.Build.0 = Release|Any CPU
169+
{C1F89961-7134-4D97-BA3A-2693FD1CBF4E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
170+
{C1F89961-7134-4D97-BA3A-2693FD1CBF4E}.Debug|Any CPU.Build.0 = Debug|Any CPU
171+
{C1F89961-7134-4D97-BA3A-2693FD1CBF4E}.Release|Any CPU.ActiveCfg = Release|Any CPU
172+
{C1F89961-7134-4D97-BA3A-2693FD1CBF4E}.Release|Any CPU.Build.0 = Release|Any CPU
173+
{9A22600C-7768-4D16-B67D-514F55942FAF}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
174+
{9A22600C-7768-4D16-B67D-514F55942FAF}.Debug|Any CPU.Build.0 = Debug|Any CPU
175+
{9A22600C-7768-4D16-B67D-514F55942FAF}.Release|Any CPU.ActiveCfg = Release|Any CPU
176+
{9A22600C-7768-4D16-B67D-514F55942FAF}.Release|Any CPU.Build.0 = Release|Any CPU
159177
EndGlobalSection
160178
GlobalSection(SolutionProperties) = preSolution
161179
HideSolutionNode = FALSE
@@ -179,5 +197,8 @@ Global
179197
{D87E54CC-C2D6-4AE5-806D-AE825B051C66} = {0987757E-4D09-4523-B9C9-65B1E8832AA1}
180198
{E496EED0-F8C9-4FE9-83E6-75E47A3C41A1} = {F446ED94-368F-4F67-913B-16E82CA80DFC}
181199
{A02A4245-3AEB-4549-9037-D89DFDC7E74D} = {F446ED94-368F-4F67-913B-16E82CA80DFC}
200+
{1EFC221F-35CF-4B55-BD59-240D5B808E14} = {900DD210-8500-4D89-A05D-C9526935A719}
201+
{C1F89961-7134-4D97-BA3A-2693FD1CBF4E} = {F446ED94-368F-4F67-913B-16E82CA80DFC}
202+
{9A22600C-7768-4D16-B67D-514F55942FAF} = {F446ED94-368F-4F67-913B-16E82CA80DFC}
182203
EndGlobalSection
183204
EndGlobal
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
namespace Microsoft.Examples.Controllers
2+
{
3+
using Microsoft.AspNetCore.Mvc;
4+
using System;
5+
using System.Collections.Generic;
6+
using System.Linq;
7+
using System.Threading.Tasks;
8+
9+
[Route( "api/v{version:apiVersion}/[controller]" )]
10+
public class HelloWorldController : Controller
11+
{
12+
// GET api/v{version}/helloworld
13+
[HttpGet]
14+
public string Get() => $"Controller = {GetType().Name}\nVersion = {HttpContext.GetRequestedApiVersion()}";
15+
16+
// GET api/v{version}/helloworld/{id}
17+
[HttpGet( "{id:int}" )]
18+
public string Get( int id ) => $"Controller = {GetType().Name}\nId = {id}\nVersion = {HttpContext.GetRequestedApiVersion()}";
19+
}
20+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
namespace Microsoft.Examples.Controllers
2+
{
3+
using Microsoft.AspNetCore.Mvc;
4+
using System;
5+
using System.Collections.Generic;
6+
using System.Linq;
7+
using System.Threading.Tasks;
8+
9+
[Route( "api/values" )]
10+
public class Values2Controller : Controller
11+
{
12+
// GET api/values?api-version=2.0
13+
[HttpGet]
14+
public string Get() => $"Controller = {GetType().Name}\nVersion = {HttpContext.GetRequestedApiVersion()}";
15+
16+
// GET api/values/{id}?api-version=2.0
17+
[HttpGet( "{id:int}" )]
18+
public string Get( int id ) => $"Controller = {GetType().Name}\nId = {id}\nVersion = {HttpContext.GetRequestedApiVersion()}";
19+
20+
// GET api/values?api-version=3.0
21+
[HttpGet]
22+
public string GetV3() => $"Controller = {GetType().Name}\nVersion = {HttpContext.GetRequestedApiVersion()}";
23+
24+
// GET api/values/{id}?api-version=3.0
25+
[HttpGet( "{id:int}" )]
26+
public string GetV3( int id ) => $"Controller = {GetType().Name}\nId = {id}\nVersion = {HttpContext.GetRequestedApiVersion()}";
27+
}
28+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
namespace Microsoft.Examples.Controllers
2+
{
3+
using Microsoft.AspNetCore.Mvc;
4+
using System;
5+
using System.Collections.Generic;
6+
using System.Linq;
7+
using System.Threading.Tasks;
8+
9+
[Route( "api/[controller]" )]
10+
public class ValuesController : Controller
11+
{
12+
// GET api/values?api-version=1.0
13+
[HttpGet]
14+
public string Get() => $"Controller = {GetType().Name}\nVersion = {HttpContext.GetRequestedApiVersion()}";
15+
16+
// GET api/values/{id}?api-version=1.0
17+
[HttpGet( "{id:int}" )]
18+
public string Get( int id ) => $"Controller = {GetType().Name}\nId = {id}\nVersion = {HttpContext.GetRequestedApiVersion()}";
19+
}
20+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<PropertyGroup>
4+
<VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">14.0</VisualStudioVersion>
5+
<VSToolsPath Condition="'$(VSToolsPath)' == ''">$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)</VSToolsPath>
6+
</PropertyGroup>
7+
<Import Project="$(VSToolsPath)\DotNet\Microsoft.DotNet.Props" Condition="'$(VSToolsPath)' != ''" />
8+
<PropertyGroup Label="Globals">
9+
<ProjectGuid>1efc221f-35cf-4b55-bd59-240d5b808e14</ProjectGuid>
10+
<RootNamespace>Microsoft.Examples</RootNamespace>
11+
<BaseIntermediateOutputPath Condition="'$(BaseIntermediateOutputPath)'=='' ">.\obj</BaseIntermediateOutputPath>
12+
<OutputPath Condition="'$(OutputPath)'=='' ">.\bin\</OutputPath>
13+
<TargetFrameworkVersion>v4.6.1</TargetFrameworkVersion>
14+
</PropertyGroup>
15+
<PropertyGroup>
16+
<SchemaVersion>2.0</SchemaVersion>
17+
</PropertyGroup>
18+
<Import Project="$(VSToolsPath)\DotNet.Web\Microsoft.DotNet.Web.targets" Condition="'$(VSToolsPath)' != ''" />
19+
</Project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
namespace Microsoft.Examples
2+
{
3+
using System;
4+
using System.Collections.Generic;
5+
using System.IO;
6+
using System.Linq;
7+
using System.Threading.Tasks;
8+
using Microsoft.AspNetCore.Hosting;
9+
using Microsoft.AspNetCore.Builder;
10+
11+
public class Program
12+
{
13+
public static void Main(string[] args)
14+
{
15+
var host = new WebHostBuilder()
16+
.UseKestrel()
17+
.UseContentRoot(Directory.GetCurrentDirectory())
18+
.UseIISIntegration()
19+
.UseStartup<Startup>()
20+
.Build();
21+
22+
host.Run();
23+
}
24+
}
25+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
{
2+
"iisSettings": {
3+
"windowsAuthentication": false,
4+
"anonymousAuthentication": true,
5+
"iisExpress": {
6+
"applicationUrl": "http://localhost:2645/",
7+
"sslPort": 0
8+
}
9+
},
10+
"profiles": {
11+
"IIS Express": {
12+
"commandName": "IISExpress",
13+
"launchBrowser": true,
14+
"launchUrl": "api/values?api-version=1.0",
15+
"environmentVariables": {
16+
"ASPNETCORE_ENVIRONMENT": "Development"
17+
}
18+
},
19+
"ConventionsSamples": {
20+
"commandName": "Project",
21+
"launchBrowser": true,
22+
"launchUrl": "http://localhost:5000/api/values",
23+
"environmentVariables": {
24+
"ASPNETCORE_ENVIRONMENT": "Development"
25+
}
26+
}
27+
}
28+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
namespace Microsoft.Examples
2+
{
3+
using Controllers;
4+
using Microsoft.AspNetCore.Builder;
5+
using Microsoft.AspNetCore.Hosting;
6+
using Microsoft.AspNetCore.Mvc.Versioning.Conventions;
7+
using Microsoft.Extensions.Configuration;
8+
using Microsoft.Extensions.DependencyInjection;
9+
using Microsoft.Extensions.Logging;
10+
using System;
11+
using System.Collections.Generic;
12+
using System.Linq;
13+
using System.Threading.Tasks;
14+
15+
public class Startup
16+
{
17+
public Startup( IHostingEnvironment env )
18+
{
19+
var builder = new ConfigurationBuilder()
20+
.SetBasePath( env.ContentRootPath )
21+
.AddJsonFile( "appsettings.json", optional: true, reloadOnChange: true )
22+
.AddJsonFile( $"appsettings.{env.EnvironmentName}.json", optional: true )
23+
.AddEnvironmentVariables();
24+
Configuration = builder.Build();
25+
}
26+
27+
public IConfigurationRoot Configuration { get; }
28+
29+
public void ConfigureServices( IServiceCollection services )
30+
{
31+
services.AddMvc();
32+
services.AddApiVersioning(
33+
options =>
34+
{
35+
// reporting api versions will return the headers "api-supported-versions" and "api-deprecated-versions"
36+
options.ReportApiVersions = true;
37+
38+
// apply api versions using conventions rather than attributes
39+
options.Conventions.Controller<ValuesController>().HasApiVersion( 1, 0 );
40+
options.Conventions.Controller<Values2Controller>()
41+
.HasApiVersion( 2, 0 )
42+
.HasApiVersion( 3, 0 )
43+
.Action( c => c.GetV3() ).MapToApiVersion( 3, 0 )
44+
.Action( c => c.GetV3( default( int ) ) ).MapToApiVersion( 3, 0 );
45+
options.Conventions.Controller<HelloWorldController>()
46+
.HasApiVersion( 1, 0 )
47+
.HasApiVersion( 2, 0 )
48+
.AdvertisesApiVersion( 3, 0 );
49+
} );
50+
}
51+
52+
public void Configure( IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory )
53+
{
54+
loggerFactory.AddConsole( Configuration.GetSection( "Logging" ) );
55+
loggerFactory.AddDebug();
56+
app.UseMvc();
57+
}
58+
}
59+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"Logging": {
3+
"IncludeScopes": false,
4+
"LogLevel": {
5+
"Default": "Debug",
6+
"System": "Information",
7+
"Microsoft": "Information"
8+
}
9+
}
10+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
{
2+
"dependencies": {
3+
"Microsoft.NETCore.App": {
4+
"version": "1.0.0",
5+
"type": "platform"
6+
},
7+
"Microsoft.AspNetCore.Mvc": "1.0.0",
8+
"Microsoft.AspNetCore.Server.IISIntegration": "1.0.0",
9+
"Microsoft.AspNetCore.Server.Kestrel": "1.0.0",
10+
"Microsoft.Extensions.Configuration.EnvironmentVariables": "1.0.0",
11+
"Microsoft.Extensions.Configuration.FileExtensions": "1.0.0",
12+
"Microsoft.Extensions.Configuration.Json": "1.0.0",
13+
"Microsoft.Extensions.Logging": "1.0.0",
14+
"Microsoft.Extensions.Logging.Console": "1.0.0",
15+
"Microsoft.Extensions.Logging.Debug": "1.0.0",
16+
"Microsoft.Extensions.Options.ConfigurationExtensions": "1.0.0",
17+
"Microsoft.AspNetCore.Mvc.Versioning": {
18+
"target": "project",
19+
"version": ""
20+
}
21+
},
22+
23+
"tools": {
24+
"Microsoft.AspNetCore.Server.IISIntegration.Tools": "1.0.0-preview2-final"
25+
},
26+
27+
"frameworks": {
28+
"netcoreapp1.0": {
29+
"imports": [
30+
"dotnet5.6",
31+
"portable-net45+win8"
32+
]
33+
}
34+
},
35+
36+
"buildOptions": {
37+
"emitEntryPoint": true,
38+
"preserveCompilationContext": true
39+
},
40+
41+
"runtimeOptions": {
42+
"configProperties": {
43+
"System.GC.Server": true
44+
}
45+
},
46+
47+
"publishOptions": {
48+
"include": [
49+
"wwwroot",
50+
"Views",
51+
"Areas/**/Views",
52+
"appsettings.json",
53+
"web.config"
54+
]
55+
},
56+
57+
"scripts": {
58+
"postpublish": [ "dotnet publish-iis --publish-folder %publish:OutputPath% --framework %publish:FullTargetFramework%" ]
59+
}
60+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<configuration>
3+
4+
<!--
5+
Configure your application settings in appsettings.json. Learn more at http://go.microsoft.com/fwlink/?LinkId=786380
6+
-->
7+
8+
<system.webServer>
9+
<handlers>
10+
<add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModule" resourceType="Unspecified"/>
11+
</handlers>
12+
<aspNetCore processPath="%LAUNCHER_PATH%" arguments="%LAUNCHER_ARGS%" stdoutLogEnabled="false" stdoutLogFile=".\logs\stdout" forwardWindowsAuthToken="false"/>
13+
</system.webServer>
14+
</configuration>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
namespace Microsoft.Examples.Configuration
2+
{
3+
using Microsoft.Web.Http;
4+
using Microsoft.Web.OData.Builder;
5+
using Models;
6+
using System.Web.OData.Builder;
7+
8+
public class OrderModelConfiguration : IModelConfiguration
9+
{
10+
private static readonly ApiVersion V1 = new ApiVersion( 1, 0 );
11+
12+
private EntityTypeConfiguration<Order> ConfigureCurrent( ODataModelBuilder builder )
13+
{
14+
var order = builder.EntitySet<Order>( "Orders" ).EntityType;
15+
16+
order.HasKey( p => p.Id );
17+
18+
return order;
19+
}
20+
21+
public void Apply( ODataModelBuilder builder, ApiVersion apiVersion )
22+
{
23+
// note: the EDM for orders is only available in version 1.0
24+
if ( apiVersion == V1 )
25+
{
26+
ConfigureCurrent( builder );
27+
}
28+
}
29+
}
30+
}

0 commit comments

Comments
 (0)