Skip to content

Commit af682d9

Browse files
author
Chris Martinez
committed
Added "version by namespace" example for ASP.NET Core
1 parent 9575616 commit af682d9

File tree

13 files changed

+255
-0
lines changed

13 files changed

+255
-0
lines changed

ApiVersioningWithSamples.sln

+7
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,8 @@ Project("{D954291E-2A0B-460D-934E-DC6B0785DB48}") = "Test.Common", "test\Test.Co
7474
EndProject
7575
Project("{D954291E-2A0B-460D-934E-DC6B0785DB48}") = "Acceptance.Test.Shared", "test\Acceptance.Test.Shared\Acceptance.Test.Shared.shproj", "{6CDFB878-2642-4F98-AE35-621BAC581181}"
7676
EndProject
77+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ByNamespaceSample", "samples\aspnetcore\ByNamespaceSample\ByNamespaceSample.csproj", "{83B21A5B-0779-4391-9700-58AEFEBFA615}"
78+
EndProject
7779
Global
7880
GlobalSection(SharedMSBuildProjectFiles) = preSolution
7981
test\Acceptance.Test.Shared\Acceptance.Test.Shared.projitems*{6cdfb878-2642-4f98-ae35-621bac581181}*SharedItemsImports = 13
@@ -150,6 +152,10 @@ Global
150152
{4EED304C-D1A6-4866-8D7F-450D084FD25D}.Debug|Any CPU.Build.0 = Debug|Any CPU
151153
{4EED304C-D1A6-4866-8D7F-450D084FD25D}.Release|Any CPU.ActiveCfg = Release|Any CPU
152154
{4EED304C-D1A6-4866-8D7F-450D084FD25D}.Release|Any CPU.Build.0 = Release|Any CPU
155+
{83B21A5B-0779-4391-9700-58AEFEBFA615}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
156+
{83B21A5B-0779-4391-9700-58AEFEBFA615}.Debug|Any CPU.Build.0 = Debug|Any CPU
157+
{83B21A5B-0779-4391-9700-58AEFEBFA615}.Release|Any CPU.ActiveCfg = Release|Any CPU
158+
{83B21A5B-0779-4391-9700-58AEFEBFA615}.Release|Any CPU.Build.0 = Release|Any CPU
153159
EndGlobalSection
154160
GlobalSection(SolutionProperties) = preSolution
155161
HideSolutionNode = FALSE
@@ -177,5 +183,6 @@ Global
177183
{6D0E834B-6422-44CD-9A85-E3BE9DEAD1BE} = {4D5F5F21-0CB7-4B4E-A42F-732BD4AFD0FF}
178184
{F9297626-C37C-402B-AFD6-712F3E5E4D7C} = {0987757E-4D09-4523-B9C9-65B1E8832AA1}
179185
{6CDFB878-2642-4F98-AE35-621BAC581181} = {0987757E-4D09-4523-B9C9-65B1E8832AA1}
186+
{83B21A5B-0779-4391-9700-58AEFEBFA615} = {900DD210-8500-4D89-A05D-C9526935A719}
180187
EndGlobalSection
181188
EndGlobal
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<Project Sdk="Microsoft.NET.Sdk.Web">
2+
3+
<PropertyGroup>
4+
<TargetFramework>netcoreapp1.0</TargetFramework>
5+
<PreserveCompilationContext>true</PreserveCompilationContext>
6+
<AssemblyName>ByNamespaceSample</AssemblyName>
7+
<OutputType>Exe</OutputType>
8+
<PackageId>ByNamespaceSample</PackageId>
9+
<RuntimeFrameworkVersion>1.0.3</RuntimeFrameworkVersion>
10+
<PackageTargetFallback>$(PackageTargetFallback);dotnet5.6;portable-net45+win8</PackageTargetFallback>
11+
</PropertyGroup>
12+
13+
<ItemGroup>
14+
<Content Update="wwwroot\**\*;Views;Areas\**\Views;appsettings.json;web.config">
15+
<CopyToPublishDirectory>PreserveNewest</CopyToPublishDirectory>
16+
</Content>
17+
</ItemGroup>
18+
19+
<ItemGroup>
20+
<ProjectReference Include="..\..\..\src\Microsoft.AspNetCore.Mvc.Versioning\Microsoft.AspNetCore.Mvc.Versioning.csproj" />
21+
</ItemGroup>
22+
23+
<ItemGroup>
24+
<PackageReference Include="Microsoft.AspNetCore.Mvc" Version="1.1.1" />
25+
<PackageReference Include="Microsoft.AspNetCore.Server.IISIntegration" Version="1.0.1" />
26+
<PackageReference Include="Microsoft.AspNetCore.Server.Kestrel" Version="1.0.2" />
27+
<PackageReference Include="Microsoft.Extensions.Configuration.EnvironmentVariables" Version="1.0.1" />
28+
<PackageReference Include="Microsoft.Extensions.Configuration.FileExtensions" Version="1.0.1" />
29+
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="1.0.1" />
30+
<PackageReference Include="Microsoft.Extensions.Logging" Version="1.0.1" />
31+
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="1.0.1" />
32+
<PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="1.0.1" />
33+
<PackageReference Include="Microsoft.Extensions.Options.ConfigurationExtensions" Version="1.0.1" />
34+
<PackageReference Include="Microsoft.AspNetCore.Diagnostics" Version="1.0.1" />
35+
</ItemGroup>
36+
37+
</Project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.IO;
4+
using System.Linq;
5+
using System.Threading.Tasks;
6+
using Microsoft.AspNetCore.Hosting;
7+
8+
namespace Microsoft.Examples
9+
{
10+
public class Program
11+
{
12+
public static void Main( string[] args )
13+
{
14+
var host = new WebHostBuilder()
15+
.UseKestrel()
16+
.UseContentRoot( Directory.GetCurrentDirectory() )
17+
.UseIISIntegration()
18+
.UseStartup<Startup>()
19+
.Build();
20+
21+
host.Run();
22+
}
23+
}
24+
}
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:26458/",
7+
"sslPort": 0
8+
}
9+
},
10+
"profiles": {
11+
"IIS Express": {
12+
"commandName": "IISExpress",
13+
"launchBrowser": true,
14+
"launchUrl": "v1/orders/42",
15+
"environmentVariables": {
16+
"ASPNETCORE_ENVIRONMENT": "Development"
17+
}
18+
},
19+
"ByNamespaceSample": {
20+
"commandName": "Project",
21+
"launchBrowser": true,
22+
"launchUrl": "http://localhost:5000/v1/orders/42",
23+
"environmentVariables": {
24+
"ASPNETCORE_ENVIRONMENT": "Development"
25+
}
26+
}
27+
}
28+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
using Microsoft.AspNetCore.Builder;
2+
using Microsoft.AspNetCore.Hosting;
3+
using Microsoft.Extensions.DependencyInjection;
4+
using Microsoft.Extensions.Logging;
5+
6+
namespace Microsoft.Examples
7+
{
8+
public class Startup
9+
{
10+
// This method gets called by the runtime. Use this method to add services to the container.
11+
// For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
12+
public void ConfigureServices( IServiceCollection services )
13+
{
14+
services.AddMvc();
15+
16+
// reporting api versions will return the headers "api-supported-versions" and "api-deprecated-versions"
17+
services.AddApiVersioning( o => o.ReportApiVersions = true );
18+
}
19+
20+
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
21+
public void Configure( IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory )
22+
{
23+
loggerFactory.AddConsole();
24+
25+
if ( env.IsDevelopment() )
26+
{
27+
app.UseDeveloperExceptionPage();
28+
}
29+
30+
app.UseMvc();
31+
}
32+
}
33+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
namespace Microsoft.Examples.V1.Controllers
2+
{
3+
using Microsoft.AspNetCore.Mvc;
4+
using Models;
5+
6+
[ApiVersion( "1.0" )]
7+
[Route( "v{version:apiVersion}/[controller]" )]
8+
public class OrdersController : Controller
9+
{
10+
// GET ~/v1/orders/{accountId}
11+
[HttpGet( "{accountId}" )]
12+
public IActionResult Get( string accountId ) => Ok( new Order( GetType().FullName, accountId, HttpContext.GetRequestedApiVersion().ToString() ) );
13+
}
14+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
namespace Microsoft.Examples.V1.Models
2+
{
3+
using System;
4+
5+
public class Order
6+
{
7+
public Order( string controller, string accountId, string apiVersion )
8+
{
9+
Controller = controller;
10+
AccountId = accountId;
11+
ApiVersion = apiVersion;
12+
}
13+
14+
public string Controller { get; set; }
15+
16+
public string AccountId { get; set; }
17+
18+
public string ApiVersion { get; set; }
19+
}
20+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
namespace Microsoft.Examples.V2.Controllers
2+
{
3+
using Microsoft.AspNetCore.Mvc;
4+
using Models;
5+
6+
[ApiVersion( "2.0" )]
7+
[Route( "v{version:apiVersion}/[controller]" )]
8+
public class OrdersController : Controller
9+
{
10+
// GET ~/v1/orders/{accountId}
11+
[HttpGet( "{accountId}" )]
12+
public IActionResult Get( string accountId ) => Ok( new Order( GetType().FullName, accountId, HttpContext.GetRequestedApiVersion().ToString() ) );
13+
}
14+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
namespace Microsoft.Examples.V2.Models
2+
{
3+
using System;
4+
5+
public class Order
6+
{
7+
public Order( string controller, string accountId, string apiVersion )
8+
{
9+
Controller = controller;
10+
AccountId = accountId;
11+
ApiVersion = apiVersion;
12+
}
13+
14+
public string Controller { get; set; }
15+
16+
public string AccountId { get; set; }
17+
18+
public string ApiVersion { get; set; }
19+
}
20+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
namespace Microsoft.Examples.V3.Controllers
2+
{
3+
using Microsoft.AspNetCore.Mvc;
4+
using Models;
5+
6+
[ApiVersion( "3.0" )]
7+
[Route( "v{version:apiVersion}/[controller]" )]
8+
public class OrdersController : Controller
9+
{
10+
// GET ~/v1/orders/{accountId}
11+
[HttpGet( "{accountId}" )]
12+
public IActionResult Get( string accountId ) => Ok( new Order( GetType().FullName, accountId, HttpContext.GetRequestedApiVersion().ToString() ) );
13+
}
14+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
namespace Microsoft.Examples.V3.Models
2+
{
3+
using System;
4+
5+
public class Order
6+
{
7+
public Order( string controller, string accountId, string apiVersion )
8+
{
9+
Controller = controller;
10+
AccountId = accountId;
11+
ApiVersion = apiVersion;
12+
}
13+
14+
public string Controller { get; set; }
15+
16+
public string AccountId { get; set; }
17+
18+
public string ApiVersion { get; set; }
19+
}
20+
}
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,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>

0 commit comments

Comments
 (0)