From 3a590b432401473b91bb0ec05f64e6f939654b69 Mon Sep 17 00:00:00 2001 From: Ryan Brandenburg Date: Sat, 20 Jul 2019 00:21:02 -0700 Subject: [PATCH] Endpoint routing in SPA templates (#11621) * Endpoint routing in SPA templates * Revert "More doc comment additions" This reverts commit ef2c042ec73f221a07d369e0c5f9ddd22298a632. * PR feedback * PR feedback * Don't make the same mistake twice * Resolve rebase problems * Remove missing variable * MapRazorPages * Conditionalize * Use variables that exist * Fix variable names --- .../content/Angular-CSharp/Startup.cs | 29 ++++++++++++------- .../content/React-CSharp/Startup.cs | 29 +++++++++++++------ .../content/ReactRedux-CSharp/Startup.cs | 10 ++++--- 3 files changed, 45 insertions(+), 23 deletions(-) diff --git a/src/ProjectTemplates/Web.Spa.ProjectTemplates/content/Angular-CSharp/Startup.cs b/src/ProjectTemplates/Web.Spa.ProjectTemplates/content/Angular-CSharp/Startup.cs index b5d6e749b9..d6e4a775cc 100644 --- a/src/ProjectTemplates/Web.Spa.ProjectTemplates/content/Angular-CSharp/Startup.cs +++ b/src/ProjectTemplates/Web.Spa.ProjectTemplates/content/Angular-CSharp/Startup.cs @@ -36,13 +36,13 @@ namespace Company.WebApplication1 { #if (IndividualLocalAuth) services.AddDbContext(options => - #if (UseLocalDB) +#if (UseLocalDB) options.UseSqlServer( Configuration.GetConnectionString("DefaultConnection"))); - #else +#else options.UseSqlite( Configuration.GetConnectionString("DefaultConnection"))); - #endif +#endif services.AddDefaultIdentity() .AddEntityFrameworkStores(); @@ -53,8 +53,10 @@ namespace Company.WebApplication1 services.AddAuthentication() .AddIdentityServerJwt(); #endif - services.AddMvc(options => options.EnableEndpointRouting = false); - + services.AddControllersWithViews(); +#if (IndividualLocalAuth) + services.AddRazorPages(); +#endif // In production, the Angular files will be served from this directory services.AddSpaStaticFiles(configuration => { @@ -87,17 +89,24 @@ namespace Company.WebApplication1 #endif app.UseStaticFiles(); app.UseSpaStaticFiles(); -#if (IndividualLocalAuth) + app.UseRouting(); + +#if (IndividualLocalAuth) app.UseAuthentication(); app.UseIdentityServer(); #endif - - app.UseMvc(routes => +#if (!NoAuth) + app.UseAuthorization(); +#endif + app.UseEndpoints(endpoints => { - routes.MapRoute( + endpoints.MapControllerRoute( name: "default", - template: "{controller}/{action=Index}/{id?}"); + pattern: "{controller}/{action=Index}/{id?}"); +#if (IndividualLocalAuth) + endpoints.MapRazorPages(); +#endif }); app.UseSpa(spa => diff --git a/src/ProjectTemplates/Web.Spa.ProjectTemplates/content/React-CSharp/Startup.cs b/src/ProjectTemplates/Web.Spa.ProjectTemplates/content/React-CSharp/Startup.cs index 2303f02e1b..13319df968 100644 --- a/src/ProjectTemplates/Web.Spa.ProjectTemplates/content/React-CSharp/Startup.cs +++ b/src/ProjectTemplates/Web.Spa.ProjectTemplates/content/React-CSharp/Startup.cs @@ -36,13 +36,13 @@ namespace Company.WebApplication1 { #if (IndividualLocalAuth) services.AddDbContext(options => - #if (UseLocalDB) +#if (UseLocalDB) options.UseSqlServer( Configuration.GetConnectionString("DefaultConnection"))); - #else +#else options.UseSqlite( Configuration.GetConnectionString("DefaultConnection"))); - #endif +#endif services.AddDefaultIdentity() .AddEntityFrameworkStores(); @@ -53,7 +53,11 @@ namespace Company.WebApplication1 services.AddAuthentication() .AddIdentityServerJwt(); #endif - services.AddMvc(options => options.EnableEndpointRouting = false); + + services.AddControllersWithViews(); +#if (IndividualLocalAuth) + services.AddRazorPages(); +#endif // In production, the React files will be served from this directory services.AddSpaStaticFiles(configuration => @@ -87,17 +91,24 @@ namespace Company.WebApplication1 #endif app.UseStaticFiles(); app.UseSpaStaticFiles(); -#if (IndividualLocalAuth) + app.UseRouting(); + +#if (IndividualLocalAuth) app.UseAuthentication(); app.UseIdentityServer(); #endif - - app.UseMvc(routes => +#if (!NoAuth) + app.UseAuthorization(); +#endif + app.UseEndpoints(endpoints => { - routes.MapRoute( + endpoints.MapControllerRoute( name: "default", - template: "{controller}/{action=Index}/{id?}"); + pattern: "{controller}/{action=Index}/{id?}"); +#if (IndividualLocalAuth) + endpoints.MapRazorPages(); +#endif }); app.UseSpa(spa => diff --git a/src/ProjectTemplates/Web.Spa.ProjectTemplates/content/ReactRedux-CSharp/Startup.cs b/src/ProjectTemplates/Web.Spa.ProjectTemplates/content/ReactRedux-CSharp/Startup.cs index 620d39f9af..2281a7ed77 100644 --- a/src/ProjectTemplates/Web.Spa.ProjectTemplates/content/ReactRedux-CSharp/Startup.cs +++ b/src/ProjectTemplates/Web.Spa.ProjectTemplates/content/ReactRedux-CSharp/Startup.cs @@ -23,7 +23,7 @@ namespace Company.WebApplication1 // This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { - services.AddMvc(options => options.EnableEndpointRouting = false); + services.AddControllersWithViews(); // In production, the React files will be served from this directory services.AddSpaStaticFiles(configuration => @@ -55,11 +55,13 @@ namespace Company.WebApplication1 app.UseStaticFiles(); app.UseSpaStaticFiles(); - app.UseMvc(routes => + app.UseRouting(); + + app.UseEndpoints(endpoints => { - routes.MapRoute( + endpoints.MapControllerRoute( name: "default", - template: "{controller}/{action=Index}/{id?}"); + pattern: "{controller}/{action=Index}/{id?}"); }); app.UseSpa(spa =>