From 96803eea60526c1c5b7d6fdc3278b45432cfffcd Mon Sep 17 00:00:00 2001 From: DamianEdwards Date: Wed, 7 Jul 2021 15:46:36 -0700 Subject: [PATCH] Update SPA templates - Use top-level statements - Use minimal hosting APIs - #33947 #33944 --- .../content/Angular-CSharp/Program.cs | 104 ++++++++++++++--- .../content/Angular-CSharp/Startup.cs | 109 ------------------ .../content/React-CSharp/Program.cs | 104 ++++++++++++++--- .../content/React-CSharp/Startup.cs | 109 ------------------ .../test/template-baselines.json | 6 +- 5 files changed, 172 insertions(+), 260 deletions(-) delete mode 100644 src/ProjectTemplates/Web.Spa.ProjectTemplates/content/Angular-CSharp/Startup.cs delete mode 100644 src/ProjectTemplates/Web.Spa.ProjectTemplates/content/React-CSharp/Startup.cs diff --git a/src/ProjectTemplates/Web.Spa.ProjectTemplates/content/Angular-CSharp/Program.cs b/src/ProjectTemplates/Web.Spa.ProjectTemplates/content/Angular-CSharp/Program.cs index 549d0b8bb8a1..b7ff9f83e146 100644 --- a/src/ProjectTemplates/Web.Spa.ProjectTemplates/content/Angular-CSharp/Program.cs +++ b/src/ProjectTemplates/Web.Spa.ProjectTemplates/content/Angular-CSharp/Program.cs @@ -1,26 +1,92 @@ using System; -using System.Collections.Generic; -using System.Linq; -using System.Threading.Tasks; +#if (IndividualLocalAuth) +using Microsoft.AspNetCore.Authentication; +#endif +using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; +#if (IndividualLocalAuth) +using Microsoft.AspNetCore.Identity; +using Microsoft.AspNetCore.Identity.UI; +#endif +#if (IndividualLocalAuth) +using Microsoft.EntityFrameworkCore; +#endif using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; -using Microsoft.Extensions.Logging; +#if (IndividualLocalAuth) +using Company.WebApplication1.Data; +using Company.WebApplication1.Models; +#endif -namespace Company.WebApplication1 +var builder = WebApplication.CreateBuilder(args); + +// Add services to the container. +#if (IndividualLocalAuth) +var connectionString = Configuration.GetConnectionString("DefaultConnection"); +builder.Services.AddDbContext(options => +#if (UseLocalDB) + options.UseSqlServer(connectionString)); +#else + options.UseSqlite(connectionString)); +#endif +builder.Services.AddDatabaseDeveloperPageExceptionFilter(); + +builder.Services.AddDefaultIdentity(options => options.SignIn.RequireConfirmedAccount = true) + .AddEntityFrameworkStores(); + +builder.Services.AddIdentityServer() + .AddApiAuthorization(); + +builder.Services.AddAuthentication() + .AddIdentityServerJwt(); +#endif + +builder.Services.AddControllersWithViews(); +#if (IndividualLocalAuth) +builder.Services.AddRazorPages(); +#endif + +var app = builder.Build(); + +// Configure the HTTP request pipeline. +if (app.Environment.IsDevelopment()) +{ + app.UseDeveloperExceptionPage(); +#if (IndividualLocalAuth) + app.UseMigrationsEndPoint(); +#endif +} +else { - public class Program - { - public static void Main(string[] args) - { - CreateHostBuilder(args).Build().Run(); - } - - public static IHostBuilder CreateHostBuilder(string[] args) => - Host.CreateDefaultBuilder(args) - .ConfigureWebHostDefaults(webBuilder => - { - webBuilder.UseStartup(); - }); - } +#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/ProjectTemplates/Web.Spa.ProjectTemplates/content/Angular-CSharp/Startup.cs b/src/ProjectTemplates/Web.Spa.ProjectTemplates/content/Angular-CSharp/Startup.cs deleted file mode 100644 index c29f37c468be..000000000000 --- a/src/ProjectTemplates/Web.Spa.ProjectTemplates/content/Angular-CSharp/Startup.cs +++ /dev/null @@ -1,109 +0,0 @@ -#if (IndividualLocalAuth) -using Microsoft.AspNetCore.Authentication; -#endif -using Microsoft.AspNetCore.Builder; -using Microsoft.AspNetCore.Hosting; -#if (IndividualLocalAuth) -using Microsoft.AspNetCore.Identity; -using Microsoft.AspNetCore.Identity.UI; -#endif -#if (RequiresHttps) -using Microsoft.AspNetCore.HttpsPolicy; -#endif -#if (IndividualLocalAuth) -using Microsoft.EntityFrameworkCore; -using Company.WebApplication1.Data; -using Company.WebApplication1.Models; -#endif -using Microsoft.Extensions.Configuration; -using Microsoft.Extensions.DependencyInjection; -using Microsoft.Extensions.Hosting; - -namespace Company.WebApplication1 -{ - public class Startup - { - public Startup(IConfiguration configuration) - { - Configuration = configuration; - } - - public IConfiguration Configuration { get; } - - // This method gets called by the runtime. Use this method to add services to the container. - public void ConfigureServices(IServiceCollection services) - { -#if (IndividualLocalAuth) - services.AddDbContext(options => -#if (UseLocalDB) - options.UseSqlServer( - Configuration.GetConnectionString("DefaultConnection"))); -#else - options.UseSqlite( - Configuration.GetConnectionString("DefaultConnection"))); -#endif - - services.AddDatabaseDeveloperPageExceptionFilter(); - - services.AddDefaultIdentity(options => options.SignIn.RequireConfirmedAccount = true) - .AddEntityFrameworkStores(); - - services.AddIdentityServer() - .AddApiAuthorization(); - - services.AddAuthentication() - .AddIdentityServerJwt(); -#endif - - services.AddControllersWithViews(); -#if (IndividualLocalAuth) - services.AddRazorPages(); -#endif - } - - // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. - public void Configure(IApplicationBuilder app, IWebHostEnvironment env) - { - if (env.IsDevelopment()) - { - app.UseDeveloperExceptionPage(); -#if (IndividualLocalAuth) - app.UseMigrationsEndPoint(); -#endif - } - else - { -#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.UseEndpoints(endpoints => - { - endpoints.MapControllerRoute( - name: "default", - pattern: "{controller}/{action=Index}/{id?}"); -#if (IndividualLocalAuth) - endpoints.MapRazorPages(); -#endif - - endpoints.MapFallbackToFile("index.html"); - }); - } - } -} diff --git a/src/ProjectTemplates/Web.Spa.ProjectTemplates/content/React-CSharp/Program.cs b/src/ProjectTemplates/Web.Spa.ProjectTemplates/content/React-CSharp/Program.cs index 549d0b8bb8a1..b7ff9f83e146 100644 --- a/src/ProjectTemplates/Web.Spa.ProjectTemplates/content/React-CSharp/Program.cs +++ b/src/ProjectTemplates/Web.Spa.ProjectTemplates/content/React-CSharp/Program.cs @@ -1,26 +1,92 @@ using System; -using System.Collections.Generic; -using System.Linq; -using System.Threading.Tasks; +#if (IndividualLocalAuth) +using Microsoft.AspNetCore.Authentication; +#endif +using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; +#if (IndividualLocalAuth) +using Microsoft.AspNetCore.Identity; +using Microsoft.AspNetCore.Identity.UI; +#endif +#if (IndividualLocalAuth) +using Microsoft.EntityFrameworkCore; +#endif using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; -using Microsoft.Extensions.Logging; +#if (IndividualLocalAuth) +using Company.WebApplication1.Data; +using Company.WebApplication1.Models; +#endif -namespace Company.WebApplication1 +var builder = WebApplication.CreateBuilder(args); + +// Add services to the container. +#if (IndividualLocalAuth) +var connectionString = Configuration.GetConnectionString("DefaultConnection"); +builder.Services.AddDbContext(options => +#if (UseLocalDB) + options.UseSqlServer(connectionString)); +#else + options.UseSqlite(connectionString)); +#endif +builder.Services.AddDatabaseDeveloperPageExceptionFilter(); + +builder.Services.AddDefaultIdentity(options => options.SignIn.RequireConfirmedAccount = true) + .AddEntityFrameworkStores(); + +builder.Services.AddIdentityServer() + .AddApiAuthorization(); + +builder.Services.AddAuthentication() + .AddIdentityServerJwt(); +#endif + +builder.Services.AddControllersWithViews(); +#if (IndividualLocalAuth) +builder.Services.AddRazorPages(); +#endif + +var app = builder.Build(); + +// Configure the HTTP request pipeline. +if (app.Environment.IsDevelopment()) +{ + app.UseDeveloperExceptionPage(); +#if (IndividualLocalAuth) + app.UseMigrationsEndPoint(); +#endif +} +else { - public class Program - { - public static void Main(string[] args) - { - CreateHostBuilder(args).Build().Run(); - } - - public static IHostBuilder CreateHostBuilder(string[] args) => - Host.CreateDefaultBuilder(args) - .ConfigureWebHostDefaults(webBuilder => - { - webBuilder.UseStartup(); - }); - } +#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/ProjectTemplates/Web.Spa.ProjectTemplates/content/React-CSharp/Startup.cs b/src/ProjectTemplates/Web.Spa.ProjectTemplates/content/React-CSharp/Startup.cs deleted file mode 100644 index c29f37c468be..000000000000 --- a/src/ProjectTemplates/Web.Spa.ProjectTemplates/content/React-CSharp/Startup.cs +++ /dev/null @@ -1,109 +0,0 @@ -#if (IndividualLocalAuth) -using Microsoft.AspNetCore.Authentication; -#endif -using Microsoft.AspNetCore.Builder; -using Microsoft.AspNetCore.Hosting; -#if (IndividualLocalAuth) -using Microsoft.AspNetCore.Identity; -using Microsoft.AspNetCore.Identity.UI; -#endif -#if (RequiresHttps) -using Microsoft.AspNetCore.HttpsPolicy; -#endif -#if (IndividualLocalAuth) -using Microsoft.EntityFrameworkCore; -using Company.WebApplication1.Data; -using Company.WebApplication1.Models; -#endif -using Microsoft.Extensions.Configuration; -using Microsoft.Extensions.DependencyInjection; -using Microsoft.Extensions.Hosting; - -namespace Company.WebApplication1 -{ - public class Startup - { - public Startup(IConfiguration configuration) - { - Configuration = configuration; - } - - public IConfiguration Configuration { get; } - - // This method gets called by the runtime. Use this method to add services to the container. - public void ConfigureServices(IServiceCollection services) - { -#if (IndividualLocalAuth) - services.AddDbContext(options => -#if (UseLocalDB) - options.UseSqlServer( - Configuration.GetConnectionString("DefaultConnection"))); -#else - options.UseSqlite( - Configuration.GetConnectionString("DefaultConnection"))); -#endif - - services.AddDatabaseDeveloperPageExceptionFilter(); - - services.AddDefaultIdentity(options => options.SignIn.RequireConfirmedAccount = true) - .AddEntityFrameworkStores(); - - services.AddIdentityServer() - .AddApiAuthorization(); - - services.AddAuthentication() - .AddIdentityServerJwt(); -#endif - - services.AddControllersWithViews(); -#if (IndividualLocalAuth) - services.AddRazorPages(); -#endif - } - - // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. - public void Configure(IApplicationBuilder app, IWebHostEnvironment env) - { - if (env.IsDevelopment()) - { - app.UseDeveloperExceptionPage(); -#if (IndividualLocalAuth) - app.UseMigrationsEndPoint(); -#endif - } - else - { -#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.UseEndpoints(endpoints => - { - endpoints.MapControllerRoute( - name: "default", - pattern: "{controller}/{action=Index}/{id?}"); -#if (IndividualLocalAuth) - endpoints.MapRazorPages(); -#endif - - endpoints.MapFallbackToFile("index.html"); - }); - } - } -} diff --git a/src/ProjectTemplates/test/template-baselines.json b/src/ProjectTemplates/test/template-baselines.json index fc4797aead50..a149fd5afd52 100644 --- a/src/ProjectTemplates/test/template-baselines.json +++ b/src/ProjectTemplates/test/template-baselines.json @@ -1180,8 +1180,7 @@ ".gitignore", "appsettings.Development.json", "appsettings.json", - "Program.cs", - "Startup.cs" + "Program.cs" ] } }, @@ -1222,8 +1221,7 @@ ".gitignore", "appsettings.Development.json", "appsettings.json", - "Program.cs", - "Startup.cs" + "Program.cs" ] } }