-
Notifications
You must be signed in to change notification settings - Fork 708
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added "version by namespace" example for ASP.NET Core
- Loading branch information
Chris Martinez
committed
Mar 10, 2017
1 parent
9575616
commit af682d9
Showing
13 changed files
with
255 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
samples/aspnetcore/ByNamespaceSample/ByNamespaceSample.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
<Project Sdk="Microsoft.NET.Sdk.Web"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>netcoreapp1.0</TargetFramework> | ||
<PreserveCompilationContext>true</PreserveCompilationContext> | ||
<AssemblyName>ByNamespaceSample</AssemblyName> | ||
<OutputType>Exe</OutputType> | ||
<PackageId>ByNamespaceSample</PackageId> | ||
<RuntimeFrameworkVersion>1.0.3</RuntimeFrameworkVersion> | ||
<PackageTargetFallback>$(PackageTargetFallback);dotnet5.6;portable-net45+win8</PackageTargetFallback> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<Content Update="wwwroot\**\*;Views;Areas\**\Views;appsettings.json;web.config"> | ||
<CopyToPublishDirectory>PreserveNewest</CopyToPublishDirectory> | ||
</Content> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\..\..\src\Microsoft.AspNetCore.Mvc.Versioning\Microsoft.AspNetCore.Mvc.Versioning.csproj" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Microsoft.AspNetCore.Mvc" Version="1.1.1" /> | ||
<PackageReference Include="Microsoft.AspNetCore.Server.IISIntegration" Version="1.0.1" /> | ||
<PackageReference Include="Microsoft.AspNetCore.Server.Kestrel" Version="1.0.2" /> | ||
<PackageReference Include="Microsoft.Extensions.Configuration.EnvironmentVariables" Version="1.0.1" /> | ||
<PackageReference Include="Microsoft.Extensions.Configuration.FileExtensions" Version="1.0.1" /> | ||
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="1.0.1" /> | ||
<PackageReference Include="Microsoft.Extensions.Logging" Version="1.0.1" /> | ||
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="1.0.1" /> | ||
<PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="1.0.1" /> | ||
<PackageReference Include="Microsoft.Extensions.Options.ConfigurationExtensions" Version="1.0.1" /> | ||
<PackageReference Include="Microsoft.AspNetCore.Diagnostics" Version="1.0.1" /> | ||
</ItemGroup> | ||
|
||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<Startup>() | ||
.Build(); | ||
|
||
host.Run(); | ||
} | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
samples/aspnetcore/ByNamespaceSample/Properties/launchSettings.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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" | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); | ||
} | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
samples/aspnetcore/ByNamespaceSample/V1/Controllers/OrdersController.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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() ) ); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; } | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
samples/aspnetcore/ByNamespaceSample/V2/Controllers/OrdersController.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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() ) ); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; } | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
samples/aspnetcore/ByNamespaceSample/V3/Controllers/OrdersController.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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() ) ); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
{ | ||
"Logging": { | ||
"IncludeScopes": false, | ||
"LogLevel": { | ||
"Default": "Debug", | ||
"System": "Information", | ||
"Microsoft": "Information" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<configuration> | ||
|
||
<!-- | ||
Configure your application settings in appsettings.json. Learn more at http://go.microsoft.com/fwlink/?LinkId=786380 | ||
--> | ||
|
||
<system.webServer> | ||
<handlers> | ||
<add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModule" resourceType="Unspecified"/> | ||
</handlers> | ||
<aspNetCore processPath="%LAUNCHER_PATH%" arguments="%LAUNCHER_ARGS%" stdoutLogEnabled="false" stdoutLogFile=".\logs\stdout" forwardWindowsAuthToken="false"/> | ||
</system.webServer> | ||
</configuration> |
af682d9
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I can't figure out how namespace influences and what.
af682d9
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The namespace does not directly play any significant role to versioning; it's purely just a way of organizing the service code. A convenience of organizing the code this way makes the division of code between versions easy to understand. It also enables type names and route templates to be the same across versions because there are no conflicts.
I suspect that you may have been expecting that the name of the namespace somehow directly influences versioning. This example doesn't and was added purely as an ASP.NET Core counterpart to the Web API samples; however it should be possible to make that work. You would need a custom IApiVersionProvider that generates version information from some part of the namespace. For ASP.NET Core, you'd have to wire this up via a IApplicationModelConvention or IControllerModelConvention, which is ultimately how both the default attribute and convention-based API versioning mechanisms inject themselves. I haven't tried it myself, but it should work.