Skip to content
This repository was archived by the owner on Mar 26, 2025. It is now read-only.

Commit 35fe714

Browse files
Add UseProgramMain template option (#42)
- Contributes to #40877
1 parent 8b90832 commit 35fe714

File tree

8 files changed

+253
-0
lines changed

8 files changed

+253
-0
lines changed

src/content/Angular-CSharp/.template.config/dotnetcli.host.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@
3636
"NoHttps": {
3737
"longName": "no-https",
3838
"shortName": ""
39+
},
40+
"UseProgramMain": {
41+
"longName": "use-program-main",
42+
"shortName": ""
3943
}
4044
},
4145
"usageExamples": [

src/content/Angular-CSharp/.template.config/template.json

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,21 @@
2828
"wwwroot/**"
2929
],
3030
"modifiers": [
31+
{
32+
"condition": "(!UseProgramMain)",
33+
"exclude": [
34+
"Program.Main.cs"
35+
]
36+
},
37+
{
38+
"condition": "(UseProgramMain)",
39+
"exclude": [
40+
"Program.cs"
41+
],
42+
"rename": {
43+
"Program.Main.cs": "Program.cs"
44+
}
45+
},
3146
{
3247
"condition": "(!IndividualLocalAuth)",
3348
"exclude": [
@@ -265,6 +280,12 @@
265280
"HostIdentifier": {
266281
"type": "bind",
267282
"binding": "HostIdentifier"
283+
},
284+
"UseProgramMain": {
285+
"type": "parameter",
286+
"datatype": "bool",
287+
"defaultValue": "false",
288+
"description": "Whether to generate an explicit Program class and Main method instead of top-level statements."
268289
}
269290
},
270291
"tags": {

src/content/Angular-CSharp/.template.config/vs-2017.3.host.json

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,18 @@
3838
"useHttps": true
3939
}
4040
],
41+
"symbolInfo": [
42+
{
43+
"id": "UseProgramMain",
44+
"name": {
45+
"text": "Use top-level statements (uncheck to use an explicit Program class with a Main method)",
46+
"overrideDefaultText": true
47+
},
48+
"invertBoolean": true,
49+
"isVisible": true,
50+
"defaultValue": true
51+
}
52+
],
4153
"excludeLaunchSettings": false,
4254
"minFullFrameworkVersion": "4.6.1",
4355
"disableHttpsSymbol": "NoHttps"
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
#if (IndividualLocalAuth)
2+
using Microsoft.AspNetCore.Authentication;
3+
using Microsoft.AspNetCore.Identity;
4+
using Microsoft.AspNetCore.Identity.UI;
5+
using Microsoft.EntityFrameworkCore;
6+
using Company.WebApplication1.Data;
7+
using Company.WebApplication1.Models;
8+
9+
#endif
10+
namespace Company.WebApplication1;
11+
12+
public class Program
13+
{
14+
public static void Main(string[] args)
15+
{
16+
var builder = WebApplication.CreateBuilder(args);
17+
18+
// Add services to the container.
19+
#if (IndividualLocalAuth)
20+
var connectionString = builder.Configuration.GetConnectionString("DefaultConnection") ?? throw new InvalidOperationException("Connection string 'DefaultConnection' not found.");
21+
builder.Services.AddDbContext<ApplicationDbContext>(options =>
22+
#if (UseLocalDB)
23+
options.UseSqlServer(connectionString));
24+
#else
25+
options.UseSqlite(connectionString));
26+
#endif
27+
builder.Services.AddDatabaseDeveloperPageExceptionFilter();
28+
29+
builder.Services.AddDefaultIdentity<ApplicationUser>(options => options.SignIn.RequireConfirmedAccount = true)
30+
.AddEntityFrameworkStores<ApplicationDbContext>();
31+
32+
builder.Services.AddIdentityServer()
33+
.AddApiAuthorization<ApplicationUser, ApplicationDbContext>();
34+
35+
builder.Services.AddAuthentication()
36+
.AddIdentityServerJwt();
37+
#endif
38+
39+
builder.Services.AddControllersWithViews();
40+
#if (IndividualLocalAuth)
41+
builder.Services.AddRazorPages();
42+
#endif
43+
44+
var app = builder.Build();
45+
46+
// Configure the HTTP request pipeline.
47+
#if (IndividualLocalAuth)
48+
if (app.Environment.IsDevelopment())
49+
{
50+
app.UseMigrationsEndPoint();
51+
}
52+
else
53+
#else
54+
if (!app.Environment.IsDevelopment())
55+
#endif
56+
{
57+
#if (RequiresHttps)
58+
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
59+
app.UseHsts();
60+
}
61+
62+
app.UseHttpsRedirection();
63+
#else
64+
}
65+
66+
#endif
67+
app.UseStaticFiles();
68+
app.UseRouting();
69+
70+
#if (IndividualLocalAuth)
71+
app.UseAuthentication();
72+
app.UseIdentityServer();
73+
#endif
74+
#if (!NoAuth)
75+
app.UseAuthorization();
76+
#endif
77+
78+
app.MapControllerRoute(
79+
name: "default",
80+
pattern: "{controller}/{action=Index}/{id?}");
81+
#if (IndividualLocalAuth)
82+
app.MapRazorPages();
83+
#endif
84+
85+
app.MapFallbackToFile("index.html");
86+
87+
app.Run();
88+
}
89+
}

src/content/React-CSharp/.template.config/dotnetcli.host.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@
3636
"NoHttps": {
3737
"longName": "no-https",
3838
"shortName": ""
39+
},
40+
"UseProgramMain": {
41+
"longName": "use-program-main",
42+
"shortName": ""
3943
}
4044
},
4145
"usageExamples": [

src/content/React-CSharp/.template.config/template.json

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,21 @@
2828
"wwwroot/**"
2929
],
3030
"modifiers": [
31+
{
32+
"condition": "(!UseProgramMain)",
33+
"exclude": [
34+
"Program.Main.cs"
35+
]
36+
},
37+
{
38+
"condition": "(UseProgramMain)",
39+
"exclude": [
40+
"Program.cs"
41+
],
42+
"rename": {
43+
"Program.Main.cs": "Program.cs"
44+
}
45+
},
3146
{
3247
"condition": "(!IndividualLocalAuth)",
3348
"exclude": [
@@ -267,6 +282,12 @@
267282
"HostIdentifier": {
268283
"type": "bind",
269284
"binding": "HostIdentifier"
285+
},
286+
"UseProgramMain": {
287+
"type": "parameter",
288+
"datatype": "bool",
289+
"defaultValue": "false",
290+
"description": "Whether to generate an explicit Program class and Main method instead of top-level statements."
270291
}
271292
},
272293
"tags": {

src/content/React-CSharp/.template.config/vs-2017.3.host.json

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,18 @@
3838
"useHttps": true
3939
}
4040
],
41+
"symbolInfo": [
42+
{
43+
"id": "UseProgramMain",
44+
"name": {
45+
"text": "Use top-level statements (uncheck to use an explicit Program class with a Main method)",
46+
"overrideDefaultText": true
47+
},
48+
"invertBoolean": true,
49+
"isVisible": true,
50+
"defaultValue": true
51+
}
52+
],
4153
"excludeLaunchSettings": false,
4254
"minFullFrameworkVersion": "4.6.1",
4355
"disableHttpsSymbol": "NoHttps"
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
#if (IndividualLocalAuth)
2+
using Microsoft.AspNetCore.Authentication;
3+
using Microsoft.AspNetCore.Identity;
4+
using Microsoft.AspNetCore.Identity.UI;
5+
using Microsoft.EntityFrameworkCore;
6+
using Company.WebApplication1.Data;
7+
using Company.WebApplication1.Models;
8+
9+
#endif
10+
11+
namespace Company.WebApplication1;
12+
13+
public class Program
14+
{
15+
public static void Main(string[] args)
16+
{
17+
var builder = WebApplication.CreateBuilder(args);
18+
19+
// Add services to the container.
20+
#if (IndividualLocalAuth)
21+
var connectionString = builder.Configuration.GetConnectionString("DefaultConnection") ?? throw new InvalidOperationException("Connection string 'DefaultConnection' not found.");
22+
builder.Services.AddDbContext<ApplicationDbContext>(options =>
23+
#if (UseLocalDB)
24+
options.UseSqlServer(connectionString));
25+
#else
26+
options.UseSqlite(connectionString));
27+
#endif
28+
builder.Services.AddDatabaseDeveloperPageExceptionFilter();
29+
30+
builder.Services.AddDefaultIdentity<ApplicationUser>(options => options.SignIn.RequireConfirmedAccount = true)
31+
.AddEntityFrameworkStores<ApplicationDbContext>();
32+
33+
builder.Services.AddIdentityServer()
34+
.AddApiAuthorization<ApplicationUser, ApplicationDbContext>();
35+
36+
builder.Services.AddAuthentication()
37+
.AddIdentityServerJwt();
38+
#endif
39+
40+
builder.Services.AddControllersWithViews();
41+
#if (IndividualLocalAuth)
42+
builder.Services.AddRazorPages();
43+
#endif
44+
45+
var app = builder.Build();
46+
47+
// Configure the HTTP request pipeline.
48+
#if (IndividualLocalAuth)
49+
if (app.Environment.IsDevelopment())
50+
{
51+
app.UseMigrationsEndPoint();
52+
}
53+
else
54+
#else
55+
if (!app.Environment.IsDevelopment())
56+
#endif
57+
{
58+
#if (RequiresHttps)
59+
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
60+
app.UseHsts();
61+
}
62+
63+
app.UseHttpsRedirection();
64+
#else
65+
}
66+
67+
#endif
68+
app.UseStaticFiles();
69+
app.UseRouting();
70+
71+
#if (IndividualLocalAuth)
72+
app.UseAuthentication();
73+
app.UseIdentityServer();
74+
#endif
75+
#if (!NoAuth)
76+
app.UseAuthorization();
77+
#endif
78+
79+
app.MapControllerRoute(
80+
name: "default",
81+
pattern: "{controller}/{action=Index}/{id?}");
82+
#if (IndividualLocalAuth)
83+
app.MapRazorPages();
84+
#endif
85+
86+
app.MapFallbackToFile("index.html");
87+
88+
app.Run();
89+
}
90+
}

0 commit comments

Comments
 (0)