From af682d94b561d98980eb16df833d4f896cd297df Mon Sep 17 00:00:00 2001 From: Chris Martinez Date: Fri, 10 Mar 2017 07:11:20 -0800 Subject: [PATCH] Added "version by namespace" example for ASP.NET Core --- ApiVersioningWithSamples.sln | 7 ++++ .../ByNamespaceSample.csproj | 37 +++++++++++++++++++ .../aspnetcore/ByNamespaceSample/Program.cs | 24 ++++++++++++ .../Properties/launchSettings.json | 28 ++++++++++++++ .../aspnetcore/ByNamespaceSample/Startup.cs | 33 +++++++++++++++++ .../V1/Controllers/OrdersController.cs | 14 +++++++ .../ByNamespaceSample/V1/Models/Order.cs | 20 ++++++++++ .../V2/Controllers/OrdersController.cs | 14 +++++++ .../ByNamespaceSample/V2/Models/Order.cs | 20 ++++++++++ .../V3/Controllers/OrdersController.cs | 14 +++++++ .../ByNamespaceSample/V3/Models/Order.cs | 20 ++++++++++ .../ByNamespaceSample/appsettings.json | 10 +++++ .../aspnetcore/ByNamespaceSample/web.config | 14 +++++++ 13 files changed, 255 insertions(+) create mode 100644 samples/aspnetcore/ByNamespaceSample/ByNamespaceSample.csproj create mode 100644 samples/aspnetcore/ByNamespaceSample/Program.cs create mode 100644 samples/aspnetcore/ByNamespaceSample/Properties/launchSettings.json create mode 100644 samples/aspnetcore/ByNamespaceSample/Startup.cs create mode 100644 samples/aspnetcore/ByNamespaceSample/V1/Controllers/OrdersController.cs create mode 100644 samples/aspnetcore/ByNamespaceSample/V1/Models/Order.cs create mode 100644 samples/aspnetcore/ByNamespaceSample/V2/Controllers/OrdersController.cs create mode 100644 samples/aspnetcore/ByNamespaceSample/V2/Models/Order.cs create mode 100644 samples/aspnetcore/ByNamespaceSample/V3/Controllers/OrdersController.cs create mode 100644 samples/aspnetcore/ByNamespaceSample/V3/Models/Order.cs create mode 100644 samples/aspnetcore/ByNamespaceSample/appsettings.json create mode 100644 samples/aspnetcore/ByNamespaceSample/web.config diff --git a/ApiVersioningWithSamples.sln b/ApiVersioningWithSamples.sln index 2e129e63..34ea74f4 100644 --- a/ApiVersioningWithSamples.sln +++ b/ApiVersioningWithSamples.sln @@ -74,6 +74,8 @@ Project("{D954291E-2A0B-460D-934E-DC6B0785DB48}") = "Test.Common", "test\Test.Co EndProject Project("{D954291E-2A0B-460D-934E-DC6B0785DB48}") = "Acceptance.Test.Shared", "test\Acceptance.Test.Shared\Acceptance.Test.Shared.shproj", "{6CDFB878-2642-4F98-AE35-621BAC581181}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ByNamespaceSample", "samples\aspnetcore\ByNamespaceSample\ByNamespaceSample.csproj", "{83B21A5B-0779-4391-9700-58AEFEBFA615}" +EndProject Global GlobalSection(SharedMSBuildProjectFiles) = preSolution test\Acceptance.Test.Shared\Acceptance.Test.Shared.projitems*{6cdfb878-2642-4f98-ae35-621bac581181}*SharedItemsImports = 13 @@ -150,6 +152,10 @@ Global {4EED304C-D1A6-4866-8D7F-450D084FD25D}.Debug|Any CPU.Build.0 = Debug|Any CPU {4EED304C-D1A6-4866-8D7F-450D084FD25D}.Release|Any CPU.ActiveCfg = Release|Any CPU {4EED304C-D1A6-4866-8D7F-450D084FD25D}.Release|Any CPU.Build.0 = Release|Any CPU + {83B21A5B-0779-4391-9700-58AEFEBFA615}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {83B21A5B-0779-4391-9700-58AEFEBFA615}.Debug|Any CPU.Build.0 = Debug|Any CPU + {83B21A5B-0779-4391-9700-58AEFEBFA615}.Release|Any CPU.ActiveCfg = Release|Any CPU + {83B21A5B-0779-4391-9700-58AEFEBFA615}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -177,5 +183,6 @@ Global {6D0E834B-6422-44CD-9A85-E3BE9DEAD1BE} = {4D5F5F21-0CB7-4B4E-A42F-732BD4AFD0FF} {F9297626-C37C-402B-AFD6-712F3E5E4D7C} = {0987757E-4D09-4523-B9C9-65B1E8832AA1} {6CDFB878-2642-4F98-AE35-621BAC581181} = {0987757E-4D09-4523-B9C9-65B1E8832AA1} + {83B21A5B-0779-4391-9700-58AEFEBFA615} = {900DD210-8500-4D89-A05D-C9526935A719} EndGlobalSection EndGlobal diff --git a/samples/aspnetcore/ByNamespaceSample/ByNamespaceSample.csproj b/samples/aspnetcore/ByNamespaceSample/ByNamespaceSample.csproj new file mode 100644 index 00000000..9546b546 --- /dev/null +++ b/samples/aspnetcore/ByNamespaceSample/ByNamespaceSample.csproj @@ -0,0 +1,37 @@ + + + + netcoreapp1.0 + true + ByNamespaceSample + Exe + ByNamespaceSample + 1.0.3 + $(PackageTargetFallback);dotnet5.6;portable-net45+win8 + + + + + PreserveNewest + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/samples/aspnetcore/ByNamespaceSample/Program.cs b/samples/aspnetcore/ByNamespaceSample/Program.cs new file mode 100644 index 00000000..ce02b86a --- /dev/null +++ b/samples/aspnetcore/ByNamespaceSample/Program.cs @@ -0,0 +1,24 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Threading.Tasks; +using Microsoft.AspNetCore.Hosting; + +namespace Microsoft.Examples +{ + public class Program + { + public static void Main( string[] args ) + { + var host = new WebHostBuilder() + .UseKestrel() + .UseContentRoot( Directory.GetCurrentDirectory() ) + .UseIISIntegration() + .UseStartup() + .Build(); + + host.Run(); + } + } +} \ No newline at end of file diff --git a/samples/aspnetcore/ByNamespaceSample/Properties/launchSettings.json b/samples/aspnetcore/ByNamespaceSample/Properties/launchSettings.json new file mode 100644 index 00000000..c3f0f2c9 --- /dev/null +++ b/samples/aspnetcore/ByNamespaceSample/Properties/launchSettings.json @@ -0,0 +1,28 @@ +{ + "iisSettings": { + "windowsAuthentication": false, + "anonymousAuthentication": true, + "iisExpress": { + "applicationUrl": "http://localhost:26458/", + "sslPort": 0 + } + }, + "profiles": { + "IIS Express": { + "commandName": "IISExpress", + "launchBrowser": true, + "launchUrl": "v1/orders/42", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + }, + "ByNamespaceSample": { + "commandName": "Project", + "launchBrowser": true, + "launchUrl": "http://localhost:5000/v1/orders/42", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + } + } +} \ No newline at end of file diff --git a/samples/aspnetcore/ByNamespaceSample/Startup.cs b/samples/aspnetcore/ByNamespaceSample/Startup.cs new file mode 100644 index 00000000..9cdba956 --- /dev/null +++ b/samples/aspnetcore/ByNamespaceSample/Startup.cs @@ -0,0 +1,33 @@ +using Microsoft.AspNetCore.Builder; +using Microsoft.AspNetCore.Hosting; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Logging; + +namespace Microsoft.Examples +{ + public class Startup + { + // This method gets called by the runtime. Use this method to add services to the container. + // For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940 + public void ConfigureServices( IServiceCollection services ) + { + services.AddMvc(); + + // reporting api versions will return the headers "api-supported-versions" and "api-deprecated-versions" + services.AddApiVersioning( o => o.ReportApiVersions = true ); + } + + // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. + public void Configure( IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory ) + { + loggerFactory.AddConsole(); + + if ( env.IsDevelopment() ) + { + app.UseDeveloperExceptionPage(); + } + + app.UseMvc(); + } + } +} \ No newline at end of file diff --git a/samples/aspnetcore/ByNamespaceSample/V1/Controllers/OrdersController.cs b/samples/aspnetcore/ByNamespaceSample/V1/Controllers/OrdersController.cs new file mode 100644 index 00000000..1cd34b12 --- /dev/null +++ b/samples/aspnetcore/ByNamespaceSample/V1/Controllers/OrdersController.cs @@ -0,0 +1,14 @@ +namespace Microsoft.Examples.V1.Controllers +{ + using Microsoft.AspNetCore.Mvc; + using Models; + + [ApiVersion( "1.0" )] + [Route( "v{version:apiVersion}/[controller]" )] + public class OrdersController : Controller + { + // GET ~/v1/orders/{accountId} + [HttpGet( "{accountId}" )] + public IActionResult Get( string accountId ) => Ok( new Order( GetType().FullName, accountId, HttpContext.GetRequestedApiVersion().ToString() ) ); + } +} \ No newline at end of file diff --git a/samples/aspnetcore/ByNamespaceSample/V1/Models/Order.cs b/samples/aspnetcore/ByNamespaceSample/V1/Models/Order.cs new file mode 100644 index 00000000..ce1b1d51 --- /dev/null +++ b/samples/aspnetcore/ByNamespaceSample/V1/Models/Order.cs @@ -0,0 +1,20 @@ +namespace Microsoft.Examples.V1.Models +{ + using System; + + public class Order + { + public Order( string controller, string accountId, string apiVersion ) + { + Controller = controller; + AccountId = accountId; + ApiVersion = apiVersion; + } + + public string Controller { get; set; } + + public string AccountId { get; set; } + + public string ApiVersion { get; set; } + } +} \ No newline at end of file diff --git a/samples/aspnetcore/ByNamespaceSample/V2/Controllers/OrdersController.cs b/samples/aspnetcore/ByNamespaceSample/V2/Controllers/OrdersController.cs new file mode 100644 index 00000000..ae8d6481 --- /dev/null +++ b/samples/aspnetcore/ByNamespaceSample/V2/Controllers/OrdersController.cs @@ -0,0 +1,14 @@ +namespace Microsoft.Examples.V2.Controllers +{ + using Microsoft.AspNetCore.Mvc; + using Models; + + [ApiVersion( "2.0" )] + [Route( "v{version:apiVersion}/[controller]" )] + public class OrdersController : Controller + { + // GET ~/v1/orders/{accountId} + [HttpGet( "{accountId}" )] + public IActionResult Get( string accountId ) => Ok( new Order( GetType().FullName, accountId, HttpContext.GetRequestedApiVersion().ToString() ) ); + } +} \ No newline at end of file diff --git a/samples/aspnetcore/ByNamespaceSample/V2/Models/Order.cs b/samples/aspnetcore/ByNamespaceSample/V2/Models/Order.cs new file mode 100644 index 00000000..87124e36 --- /dev/null +++ b/samples/aspnetcore/ByNamespaceSample/V2/Models/Order.cs @@ -0,0 +1,20 @@ +namespace Microsoft.Examples.V2.Models +{ + using System; + + public class Order + { + public Order( string controller, string accountId, string apiVersion ) + { + Controller = controller; + AccountId = accountId; + ApiVersion = apiVersion; + } + + public string Controller { get; set; } + + public string AccountId { get; set; } + + public string ApiVersion { get; set; } + } +} \ No newline at end of file diff --git a/samples/aspnetcore/ByNamespaceSample/V3/Controllers/OrdersController.cs b/samples/aspnetcore/ByNamespaceSample/V3/Controllers/OrdersController.cs new file mode 100644 index 00000000..9966d1d6 --- /dev/null +++ b/samples/aspnetcore/ByNamespaceSample/V3/Controllers/OrdersController.cs @@ -0,0 +1,14 @@ +namespace Microsoft.Examples.V3.Controllers +{ + using Microsoft.AspNetCore.Mvc; + using Models; + + [ApiVersion( "3.0" )] + [Route( "v{version:apiVersion}/[controller]" )] + public class OrdersController : Controller + { + // GET ~/v1/orders/{accountId} + [HttpGet( "{accountId}" )] + public IActionResult Get( string accountId ) => Ok( new Order( GetType().FullName, accountId, HttpContext.GetRequestedApiVersion().ToString() ) ); + } +} \ No newline at end of file diff --git a/samples/aspnetcore/ByNamespaceSample/V3/Models/Order.cs b/samples/aspnetcore/ByNamespaceSample/V3/Models/Order.cs new file mode 100644 index 00000000..279fc41f --- /dev/null +++ b/samples/aspnetcore/ByNamespaceSample/V3/Models/Order.cs @@ -0,0 +1,20 @@ +namespace Microsoft.Examples.V3.Models +{ + using System; + + public class Order + { + public Order( string controller, string accountId, string apiVersion ) + { + Controller = controller; + AccountId = accountId; + ApiVersion = apiVersion; + } + + public string Controller { get; set; } + + public string AccountId { get; set; } + + public string ApiVersion { get; set; } + } +} \ No newline at end of file diff --git a/samples/aspnetcore/ByNamespaceSample/appsettings.json b/samples/aspnetcore/ByNamespaceSample/appsettings.json new file mode 100644 index 00000000..fa8ce71a --- /dev/null +++ b/samples/aspnetcore/ByNamespaceSample/appsettings.json @@ -0,0 +1,10 @@ +{ + "Logging": { + "IncludeScopes": false, + "LogLevel": { + "Default": "Debug", + "System": "Information", + "Microsoft": "Information" + } + } +} diff --git a/samples/aspnetcore/ByNamespaceSample/web.config b/samples/aspnetcore/ByNamespaceSample/web.config new file mode 100644 index 00000000..dc0514fc --- /dev/null +++ b/samples/aspnetcore/ByNamespaceSample/web.config @@ -0,0 +1,14 @@ + + + + + + + + + + + +