Skip to content

Commit 84bd0ec

Browse files
API template updates (#49394)
* Default webapi template to use minimal APIs & introduce option to use controllers * Rename api template to webapiaot and remove aot option as it's always aot now
1 parent ff800a5 commit 84bd0ec

19 files changed

+126
-193
lines changed

src/ProjectTemplates/Shared/ArgConstants.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ namespace Templates.Test.Helpers;
66
internal static class ArgConstants
77
{
88
public const string UseProgramMain = "--use-program-main";
9-
public const string UseMinimalApis = "--use-minimal-apis";
9+
public const string UseControllers = "--use-controllers";
1010
public const string Pwa = "--pwa";
1111
public const string CallsGraph = "--calls-graph";
1212
public const string CalledApiUrl = "--called-api-url";

src/ProjectTemplates/Web.ProjectTemplates/Api-CSharp.csproj.in

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,8 @@
66
<ImplicitUsings>enable</ImplicitUsings>
77
<NoDefaultLaunchSettingsFile Condition="'$(ExcludeLaunchSettings)' == 'True'">true</NoDefaultLaunchSettingsFile>
88
<RootNamespace Condition="'$(name)' != '$(name{-VALUE-FORMS-}safe_namespace)'">Company.WebApplication1</RootNamespace>
9-
<!--#if (NativeAot) -->
109
<InvariantGlobalization>true</InvariantGlobalization>
1110
<PublishAot>true</PublishAot>
12-
<!--#endif -->
1311
</PropertyGroup>
1412

1513
</Project>

src/ProjectTemplates/Web.ProjectTemplates/content/Api-CSharp/.template.config/dotnetcli.host.json

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,6 @@
2121
"UseProgramMain": {
2222
"longName": "use-program-main",
2323
"shortName": ""
24-
},
25-
"NativeAot": {
26-
"longName": "aot",
27-
"shortName": ""
2824
}
2925
},
3026
"usageExamples": [

src/ProjectTemplates/Web.ProjectTemplates/content/Api-CSharp/.template.config/ide.host.json

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,6 @@
99
"isVisible": true,
1010
"persistenceScope": "shared",
1111
"persistenceScopeName": "Microsoft"
12-
},
13-
{
14-
"id": "NativeAot",
15-
"isVisible": true
1612
}
1713
]
1814
}

src/ProjectTemplates/Web.ProjectTemplates/content/Api-CSharp/.template.config/template.json

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,17 @@
33
"author": "Microsoft",
44
"classifications": [
55
"Web",
6-
"API"
6+
"Web API",
7+
"API",
8+
"Service"
79
],
8-
"name": "ASP.NET Core API",
10+
"name": "ASP.NET Core Web API (native AOT)",
911
"generatorVersions": "[1.0.0.0-*)",
10-
"description": "A project template for creating an ASP.NET Core API application.",
12+
"description": "A project template for creating a RESTful Web API using ASP.NET Core minimal APIs published as native AOT.",
1113
"groupIdentity": "Microsoft.Web.Api",
1214
"precedence": "9000",
1315
"identity": "Microsoft.Web.Api.CSharp.8.0",
14-
"shortName": "api",
16+
"shortName": "webapiaot",
1517
"tags": {
1618
"language": "C#",
1719
"type": "project"
@@ -128,13 +130,6 @@
128130
"defaultValue": "false",
129131
"displayName": "Do not use _top-level statements",
130132
"description": "Whether to generate an explicit Program class and Main method instead of top-level statements."
131-
},
132-
"NativeAot" : {
133-
"type": "parameter",
134-
"datatype": "bool",
135-
"defaultValue": "false",
136-
"displayName": "Enable _native AOT publish",
137-
"description": "Whether to enable the project for publishing as native AOT."
138133
}
139134
},
140135
"primaryOutputs": [

src/ProjectTemplates/Web.ProjectTemplates/content/Api-CSharp/Program.Main.cs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
1-
#if NativeAot
21
using System.Text.Json.Serialization;
32

4-
#endif
53
namespace Company.ApiApplication1;
64

75
public class Program
@@ -10,13 +8,11 @@ public static void Main(string[] args)
108
{
119
var builder = WebApplication.CreateSlimBuilder(args);
1210

13-
#if (NativeAot)
1411
builder.Services.ConfigureHttpJsonOptions(options =>
1512
{
1613
options.SerializerOptions.TypeInfoResolverChain.Insert(0, AppJsonSerializerContext.Default);
1714
});
1815

19-
#endif
2016
var app = builder.Build();
2117

2218
var sampleTodos = new Todo[] {
@@ -40,7 +36,6 @@ public static void Main(string[] args)
4036

4137
public record Todo(int Id, string? Title, DateOnly? DueBy = null, bool IsComplete = false);
4238

43-
#if (NativeAot)
4439
[JsonSerializable(typeof(Todo[]))]
4540
internal partial class AppJsonSerializerContext : JsonSerializerContext
4641
{

src/ProjectTemplates/Web.ProjectTemplates/content/Api-CSharp/Program.cs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,12 @@
1-
#if (NativeAot)
21
using System.Text.Json.Serialization;
32

4-
#endif
53
var builder = WebApplication.CreateSlimBuilder(args);
64

7-
#if (NativeAot)
85
builder.Services.ConfigureHttpJsonOptions(options =>
96
{
107
options.SerializerOptions.TypeInfoResolverChain.Insert(0, AppJsonSerializerContext.Default);
118
});
129

13-
#endif
1410
var app = builder.Build();
1511

1612
var sampleTodos = new Todo[] {
@@ -32,7 +28,6 @@
3228

3329
public record Todo(int Id, string? Title, DateOnly? DueBy = null, bool IsComplete = false);
3430

35-
#if (NativeAot)
3631
[JsonSerializable(typeof(Todo[]))]
3732
internal partial class AppJsonSerializerContext : JsonSerializerContext
3833
{

src/ProjectTemplates/Web.ProjectTemplates/content/GrpcService-CSharp/.template.config/template.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,13 @@
33
"author": "Microsoft",
44
"classifications": [
55
"Web",
6-
"gRPC"
6+
"gRPC",
7+
"API",
8+
"Service"
79
],
810
"name": "ASP.NET Core gRPC Service",
911
"generatorVersions": "[1.0.0.0-*)",
10-
"description": "A project template for creating a gRPC ASP.NET Core service.",
12+
"description": "A project template for creating a gRPC service using ASP.NET Core, with optional support for publishing as native AOT.",
1113
"groupIdentity": "Microsoft.Web.Grpc",
1214
"precedence": "9800",
1315
"identity": "Microsoft.Grpc.Service.CSharp.8.0",

src/ProjectTemplates/Web.ProjectTemplates/content/WebApi-CSharp/.template.config/dotnetcli.host.json

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,12 @@
66
},
77
"UseMinimalAPIs": {
88
"longName": "use-minimal-apis",
9-
"shortName": "minimal"
9+
"shortName": "minimal",
10+
"isHidden": true
11+
},
12+
"UseControllers": {
13+
"longName": "use-controllers",
14+
"shortName": "controllers"
1015
},
1116
"AADInstance": {
1217
"longName": "aad-instance",

src/ProjectTemplates/Web.ProjectTemplates/content/WebApi-CSharp/.template.config/ide.host.json

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,20 @@
2626
"overrideDefaultText": true
2727
},
2828
"invertBoolean": true,
29-
"isVisible": true,
29+
"isVisible": false,
3030
"defaultValue": "true",
3131
"persistenceScope": "templateGroup"
3232
},
33+
{
34+
"id": "UseControllers",
35+
"name": {
36+
"text": "Use controllers",
37+
"overrideDefaultText": true
38+
},
39+
"isVisible": true,
40+
"defaultValue": "false",
41+
"persistenceScope": "templateGroup"
42+
},
3343
{
3444
"id": "UseProgramMain",
3545
"isVisible": true,

src/ProjectTemplates/Web.ProjectTemplates/content/WebApi-CSharp/.template.config/template.json

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,13 @@
33
"author": "Microsoft",
44
"classifications": [
55
"Web",
6-
"WebAPI"
6+
"Web API",
7+
"API",
8+
"Service"
79
],
810
"name": "ASP.NET Core Web API",
911
"generatorVersions": "[1.0.0.0-*)",
10-
"description": "A project template for creating an ASP.NET Core application with an example Controller for a RESTful HTTP service. This template can also be used for ASP.NET Core MVC Views and Controllers.",
12+
"description": "A project template for creating a RESTful Web API using ASP.NET Core controllers or minimal APIs, with optional support for OpenAPI and authentication.",
1113
"groupIdentity": "Microsoft.Web.WebApi",
1214
"precedence": "9800",
1315
"identity": "Microsoft.Web.WebApi.CSharp.8.0",
@@ -55,19 +57,19 @@
5557
}
5658
},
5759
{
58-
"condition": "(UseMinimalAPIs)",
60+
"condition": "(UsingMinimalAPIs)",
5961
"exclude": [
6062
"Controllers/WeatherForecastController.cs"
6163
]
6264
},
6365
{
64-
"condition": "(UseMinimalAPIs && !UseProgramMain)",
66+
"condition": "(UsingMinimalAPIs && !UseProgramMain)",
6567
"exclude": [
6668
"WeatherForecast.cs"
6769
]
6870
},
6971
{
70-
"condition": "(!UseProgramMain && UseMinimalAPIs && (NoAuth || WindowsAuth))",
72+
"condition": "(!UseProgramMain && UsingMinimalAPIs && (NoAuth || WindowsAuth))",
7173
"exclude": [
7274
"Program.cs",
7375
"Program.MinimalAPIs.OrgOrIndividualB2CAuth.cs"
@@ -77,7 +79,7 @@
7779
}
7880
},
7981
{
80-
"condition": "(!UseProgramMain && UseMinimalAPIs && (IndividualAuth || OrganizationalAuth))",
82+
"condition": "(!UseProgramMain && UsingMinimalAPIs && (IndividualAuth || OrganizationalAuth))",
8183
"exclude": [
8284
"Program.cs",
8385
"Program.MinimalAPIs.WindowsOrNoAuth.cs"
@@ -87,7 +89,7 @@
8789
}
8890
},
8991
{
90-
"condition": "(UseControllers)",
92+
"condition": "(UsingControllers)",
9193
"exclude": [
9294
"Program.MinimalAPIs.WindowsOrNoAuth.cs",
9395
"Program.MinimalAPIs.OrgOrIndividualB2CAuth.cs"
@@ -321,9 +323,15 @@
321323
"UseMinimalAPIs": {
322324
"type": "parameter",
323325
"datatype": "bool",
324-
"defaultValue": "false",
326+
"defaultValue": "true",
325327
"description": "Whether to use minimal APIs instead of controllers."
326328
},
329+
"UseControllers": {
330+
"type": "parameter",
331+
"datatype": "bool",
332+
"defaultValue": "false",
333+
"description": "Whether to use controllers instead of minimal APIs. This option overides the value specified by --minimal."
334+
},
327335
"Framework": {
328336
"type": "parameter",
329337
"description": "The target framework for the project.",
@@ -388,13 +396,17 @@
388396
"defaultValue": "false",
389397
"description": "Disable OpenAPI (Swagger) support"
390398
},
391-
"EnableOpenAPI": {
399+
"UsingControllers": {
392400
"type": "computed",
393-
"value": "(!DisableOpenAPI)"
401+
"value": "(UseControllers || !UseMinimalAPIs)"
394402
},
395-
"UseControllers": {
403+
"UsingMinimalAPIs": {
404+
"type": "computed",
405+
"value": "(!UsingControllers)"
406+
},
407+
"EnableOpenAPI": {
396408
"type": "computed",
397-
"value": "(!UseMinimalAPIs)"
409+
"value": "(!DisableOpenAPI)"
398410
},
399411
"UseProgramMain": {
400412
"type": "parameter",

src/ProjectTemplates/Web.ProjectTemplates/content/WebApi-CSharp/Program.Main.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,11 @@ public static void Main(string[] args)
5151
.AddMicrosoftIdentityWebApi(builder.Configuration.GetSection("AzureAdB2C"));
5252
#endif
5353
#endif
54-
#if (UseMinimalAPIs)
54+
#if (UsingMinimalAPIs)
5555
builder.Services.AddAuthorization();
5656
#endif
5757

58-
#if (UseControllers)
58+
#if (UsingControllers)
5959
builder.Services.AddControllers();
6060
#endif
6161
#if (EnableOpenAPI)
@@ -92,7 +92,7 @@ public static void Main(string[] args)
9292

9393
app.UseAuthorization();
9494

95-
#if (UseMinimalAPIs)
95+
#if (UsingMinimalAPIs)
9696
#if (OrganizationalAuth || IndividualB2CAuth)
9797
var scopeRequiredByApi = app.Configuration["AzureAd:Scopes"] ?? "";
9898
#endif
@@ -178,7 +178,7 @@ public static void Main(string[] args)
178178
});
179179
#endif
180180
#endif
181-
#if (UseControllers)
181+
#if (UsingControllers)
182182

183183
app.MapControllers();
184184
#endif

src/ProjectTemplates/scripts/Run-ApiProgramMain-Locally.ps1

Lines changed: 0 additions & 12 deletions
This file was deleted.

src/ProjectTemplates/scripts/Run-ApiProgramMainNativeAot-Locally.ps1

Lines changed: 0 additions & 12 deletions
This file was deleted.

src/ProjectTemplates/scripts/Run-Api-Locally.ps1 renamed to src/ProjectTemplates/scripts/Run-WebApiAot-Locally.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,4 @@ $ErrorActionPreference = 'Stop'
99

1010
. $PSScriptRoot\Test-Template.ps1
1111

12-
Test-Template "api" "api" "Microsoft.DotNet.Web.ProjectTemplates.8.0.8.0.0-dev.nupkg" $false
12+
Test-Template "webapiaot" "webapiaot" "Microsoft.DotNet.Web.ProjectTemplates.8.0.8.0.0-dev.nupkg" $false

src/ProjectTemplates/scripts/Run-ApiNativeAot-Locally.ps1 renamed to src/ProjectTemplates/scripts/Run-WebApiAotProgramMain-Locally.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,4 @@ $ErrorActionPreference = 'Stop'
99

1010
. $PSScriptRoot\Test-Template.ps1
1111

12-
Test-Template "api" "api -aot" "Microsoft.DotNet.Web.ProjectTemplates.8.0.8.0.0-dev.nupkg" $false
12+
Test-Template "webapiaot" "webapiaot --use-program-main" "Microsoft.DotNet.Web.ProjectTemplates.8.0.8.0.0-dev.nupkg" $false

0 commit comments

Comments
 (0)