Skip to content

Commit

Permalink
Added "version by namespace" example for ASP.NET Core
Browse files Browse the repository at this point in the history
  • Loading branch information
Chris Martinez committed Mar 10, 2017
1 parent 9575616 commit af682d9
Show file tree
Hide file tree
Showing 13 changed files with 255 additions and 0 deletions.
7 changes: 7 additions & 0 deletions ApiVersioningWithSamples.sln
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
37 changes: 37 additions & 0 deletions samples/aspnetcore/ByNamespaceSample/ByNamespaceSample.csproj
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>
24 changes: 24 additions & 0 deletions samples/aspnetcore/ByNamespaceSample/Program.cs
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();
}
}
}
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"
}
}
}
}
33 changes: 33 additions & 0 deletions samples/aspnetcore/ByNamespaceSample/Startup.cs
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();
}
}
}
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() ) );
}
}
20 changes: 20 additions & 0 deletions samples/aspnetcore/ByNamespaceSample/V1/Models/Order.cs
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; }
}
}
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() ) );
}
}
20 changes: 20 additions & 0 deletions samples/aspnetcore/ByNamespaceSample/V2/Models/Order.cs
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; }
}
}
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() ) );
}
}
20 changes: 20 additions & 0 deletions samples/aspnetcore/ByNamespaceSample/V3/Models/Order.cs
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; }
}
}
10 changes: 10 additions & 0 deletions samples/aspnetcore/ByNamespaceSample/appsettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"Logging": {
"IncludeScopes": false,
"LogLevel": {
"Default": "Debug",
"System": "Information",
"Microsoft": "Information"
}
}
}
14 changes: 14 additions & 0 deletions samples/aspnetcore/ByNamespaceSample/web.config
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>

2 comments on commit af682d9

@xperiandri
Copy link

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.

@commonsensesoftware
Copy link
Collaborator

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.

Please sign in to comment.