diff options
author | Damian Edwards <damian@damianedwards.com> | 2022-03-28 19:40:47 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-03-28 19:40:47 +0300 |
commit | 35fe7145d402a9519591e99edf20d6d3c7d091bb (patch) | |
tree | 1533c5b619a2fe654dcaac2d65d29e8593d701ae | |
parent | 8b90832a77eb141d957b9c484cda6f750998bb70 (diff) |
Add UseProgramMain template option (#42)
- Contributes to #40877
8 files changed, 253 insertions, 0 deletions
diff --git a/src/content/Angular-CSharp/.template.config/dotnetcli.host.json b/src/content/Angular-CSharp/.template.config/dotnetcli.host.json index d961706..229232f 100644 --- a/src/content/Angular-CSharp/.template.config/dotnetcli.host.json +++ b/src/content/Angular-CSharp/.template.config/dotnetcli.host.json @@ -36,6 +36,10 @@ "NoHttps": { "longName": "no-https", "shortName": "" + }, + "UseProgramMain": { + "longName": "use-program-main", + "shortName": "" } }, "usageExamples": [ diff --git a/src/content/Angular-CSharp/.template.config/template.json b/src/content/Angular-CSharp/.template.config/template.json index 74a3449..aec38e0 100644 --- a/src/content/Angular-CSharp/.template.config/template.json +++ b/src/content/Angular-CSharp/.template.config/template.json @@ -29,6 +29,21 @@ ], "modifiers": [ { + "condition": "(!UseProgramMain)", + "exclude": [ + "Program.Main.cs" + ] + }, + { + "condition": "(UseProgramMain)", + "exclude": [ + "Program.cs" + ], + "rename": { + "Program.Main.cs": "Program.cs" + } + }, + { "condition": "(!IndividualLocalAuth)", "exclude": [ "Pages/Shared/_LoginPartial.cshtml", @@ -265,6 +280,12 @@ "HostIdentifier": { "type": "bind", "binding": "HostIdentifier" + }, + "UseProgramMain": { + "type": "parameter", + "datatype": "bool", + "defaultValue": "false", + "description": "Whether to generate an explicit Program class and Main method instead of top-level statements." } }, "tags": { diff --git a/src/content/Angular-CSharp/.template.config/vs-2017.3.host.json b/src/content/Angular-CSharp/.template.config/vs-2017.3.host.json index de9473c..32f363a 100644 --- a/src/content/Angular-CSharp/.template.config/vs-2017.3.host.json +++ b/src/content/Angular-CSharp/.template.config/vs-2017.3.host.json @@ -38,6 +38,18 @@ "useHttps": true } ], + "symbolInfo": [ + { + "id": "UseProgramMain", + "name": { + "text": "Use top-level statements (uncheck to use an explicit Program class with a Main method)", + "overrideDefaultText": true + }, + "invertBoolean": true, + "isVisible": true, + "defaultValue": true + } + ], "excludeLaunchSettings": false, "minFullFrameworkVersion": "4.6.1", "disableHttpsSymbol": "NoHttps" diff --git a/src/content/Angular-CSharp/Program.Main.cs b/src/content/Angular-CSharp/Program.Main.cs new file mode 100644 index 0000000..e68f881 --- /dev/null +++ b/src/content/Angular-CSharp/Program.Main.cs @@ -0,0 +1,89 @@ +#if (IndividualLocalAuth) +using Microsoft.AspNetCore.Authentication; +using Microsoft.AspNetCore.Identity; +using Microsoft.AspNetCore.Identity.UI; +using Microsoft.EntityFrameworkCore; +using Company.WebApplication1.Data; +using Company.WebApplication1.Models; + +#endif +namespace Company.WebApplication1; + +public class Program +{ + public static void Main(string[] args) + { + var builder = WebApplication.CreateBuilder(args); + + // Add services to the container. + #if (IndividualLocalAuth) + var connectionString = builder.Configuration.GetConnectionString("DefaultConnection") ?? throw new InvalidOperationException("Connection string 'DefaultConnection' not found."); + builder.Services.AddDbContext<ApplicationDbContext>(options => + #if (UseLocalDB) + options.UseSqlServer(connectionString)); + #else + options.UseSqlite(connectionString)); + #endif + builder.Services.AddDatabaseDeveloperPageExceptionFilter(); + + builder.Services.AddDefaultIdentity<ApplicationUser>(options => options.SignIn.RequireConfirmedAccount = true) + .AddEntityFrameworkStores<ApplicationDbContext>(); + + builder.Services.AddIdentityServer() + .AddApiAuthorization<ApplicationUser, ApplicationDbContext>(); + + builder.Services.AddAuthentication() + .AddIdentityServerJwt(); + #endif + + builder.Services.AddControllersWithViews(); + #if (IndividualLocalAuth) + builder.Services.AddRazorPages(); + #endif + + var app = builder.Build(); + + // Configure the HTTP request pipeline. + #if (IndividualLocalAuth) + if (app.Environment.IsDevelopment()) + { + app.UseMigrationsEndPoint(); + } + else + #else + if (!app.Environment.IsDevelopment()) + #endif + { + #if (RequiresHttps) + // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts. + app.UseHsts(); + } + + app.UseHttpsRedirection(); + #else + } + + #endif + app.UseStaticFiles(); + app.UseRouting(); + + #if (IndividualLocalAuth) + app.UseAuthentication(); + app.UseIdentityServer(); + #endif + #if (!NoAuth) + app.UseAuthorization(); + #endif + + app.MapControllerRoute( + name: "default", + pattern: "{controller}/{action=Index}/{id?}"); + #if (IndividualLocalAuth) + app.MapRazorPages(); + #endif + + app.MapFallbackToFile("index.html"); + + app.Run(); + } +} diff --git a/src/content/React-CSharp/.template.config/dotnetcli.host.json b/src/content/React-CSharp/.template.config/dotnetcli.host.json index d961706..229232f 100644 --- a/src/content/React-CSharp/.template.config/dotnetcli.host.json +++ b/src/content/React-CSharp/.template.config/dotnetcli.host.json @@ -36,6 +36,10 @@ "NoHttps": { "longName": "no-https", "shortName": "" + }, + "UseProgramMain": { + "longName": "use-program-main", + "shortName": "" } }, "usageExamples": [ diff --git a/src/content/React-CSharp/.template.config/template.json b/src/content/React-CSharp/.template.config/template.json index c8a02ad..2ccf7e8 100644 --- a/src/content/React-CSharp/.template.config/template.json +++ b/src/content/React-CSharp/.template.config/template.json @@ -29,6 +29,21 @@ ], "modifiers": [ { + "condition": "(!UseProgramMain)", + "exclude": [ + "Program.Main.cs" + ] + }, + { + "condition": "(UseProgramMain)", + "exclude": [ + "Program.cs" + ], + "rename": { + "Program.Main.cs": "Program.cs" + } + }, + { "condition": "(!IndividualLocalAuth)", "exclude": [ "Pages/Shared/_LoginPartial.cshtml", @@ -267,6 +282,12 @@ "HostIdentifier": { "type": "bind", "binding": "HostIdentifier" + }, + "UseProgramMain": { + "type": "parameter", + "datatype": "bool", + "defaultValue": "false", + "description": "Whether to generate an explicit Program class and Main method instead of top-level statements." } }, "tags": { diff --git a/src/content/React-CSharp/.template.config/vs-2017.3.host.json b/src/content/React-CSharp/.template.config/vs-2017.3.host.json index ded765a..5e79537 100644 --- a/src/content/React-CSharp/.template.config/vs-2017.3.host.json +++ b/src/content/React-CSharp/.template.config/vs-2017.3.host.json @@ -38,6 +38,18 @@ "useHttps": true } ], + "symbolInfo": [ + { + "id": "UseProgramMain", + "name": { + "text": "Use top-level statements (uncheck to use an explicit Program class with a Main method)", + "overrideDefaultText": true + }, + "invertBoolean": true, + "isVisible": true, + "defaultValue": true + } + ], "excludeLaunchSettings": false, "minFullFrameworkVersion": "4.6.1", "disableHttpsSymbol": "NoHttps" diff --git a/src/content/React-CSharp/Program.Main.cs b/src/content/React-CSharp/Program.Main.cs new file mode 100644 index 0000000..bd5dc64 --- /dev/null +++ b/src/content/React-CSharp/Program.Main.cs @@ -0,0 +1,90 @@ +#if (IndividualLocalAuth) +using Microsoft.AspNetCore.Authentication; +using Microsoft.AspNetCore.Identity; +using Microsoft.AspNetCore.Identity.UI; +using Microsoft.EntityFrameworkCore; +using Company.WebApplication1.Data; +using Company.WebApplication1.Models; + +#endif + +namespace Company.WebApplication1; + +public class Program +{ + public static void Main(string[] args) + { + var builder = WebApplication.CreateBuilder(args); + + // Add services to the container. + #if (IndividualLocalAuth) + var connectionString = builder.Configuration.GetConnectionString("DefaultConnection") ?? throw new InvalidOperationException("Connection string 'DefaultConnection' not found."); + builder.Services.AddDbContext<ApplicationDbContext>(options => + #if (UseLocalDB) + options.UseSqlServer(connectionString)); + #else + options.UseSqlite(connectionString)); + #endif + builder.Services.AddDatabaseDeveloperPageExceptionFilter(); + + builder.Services.AddDefaultIdentity<ApplicationUser>(options => options.SignIn.RequireConfirmedAccount = true) + .AddEntityFrameworkStores<ApplicationDbContext>(); + + builder.Services.AddIdentityServer() + .AddApiAuthorization<ApplicationUser, ApplicationDbContext>(); + + builder.Services.AddAuthentication() + .AddIdentityServerJwt(); + #endif + + builder.Services.AddControllersWithViews(); + #if (IndividualLocalAuth) + builder.Services.AddRazorPages(); + #endif + + var app = builder.Build(); + + // Configure the HTTP request pipeline. + #if (IndividualLocalAuth) + if (app.Environment.IsDevelopment()) + { + app.UseMigrationsEndPoint(); + } + else + #else + if (!app.Environment.IsDevelopment()) + #endif + { + #if (RequiresHttps) + // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts. + app.UseHsts(); + } + + app.UseHttpsRedirection(); + #else + } + + #endif + app.UseStaticFiles(); + app.UseRouting(); + + #if (IndividualLocalAuth) + app.UseAuthentication(); + app.UseIdentityServer(); + #endif + #if (!NoAuth) + app.UseAuthorization(); + #endif + + app.MapControllerRoute( + name: "default", + pattern: "{controller}/{action=Index}/{id?}"); + #if (IndividualLocalAuth) + app.MapRazorPages(); + #endif + + app.MapFallbackToFile("index.html"); + + app.Run(); + } +} |