Skip to content

Commit

Permalink
Backport UseProgramMain template option (#45)
Browse files Browse the repository at this point in the history
Contributes to #40945
  • Loading branch information
DamianEdwards authored Apr 6, 2022
1 parent 3d16ec4 commit 08a4e74
Show file tree
Hide file tree
Showing 8 changed files with 243 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@
"NoHttps": {
"longName": "no-https",
"shortName": ""
},
"UseProgramMain": {
"longName": "use-program-main",
"shortName": ""
}
},
"usageExamples": [
Expand Down
22 changes: 22 additions & 0 deletions src/content/Angular-CSharp/.template.config/template.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,21 @@
"wwwroot/**"
],
"modifiers": [
{
"condition": "(!UseProgramMain)",
"exclude": [
"Program.Main.cs"
]
},
{
"condition": "(UseProgramMain)",
"exclude": [
"Program.cs"
],
"rename": {
"Program.Main.cs": "Program.cs"
}
},
{
"condition": "(!IndividualLocalAuth)",
"exclude": [
Expand Down Expand Up @@ -265,6 +280,13 @@
"HostIdentifier": {
"type": "bind",
"binding": "HostIdentifier"
},
"UseProgramMain": {
"type": "parameter",
"datatype": "bool",
"defaultValue": "false",
"displayName": "Do not use top-level statements",
"description": "Whether to generate an explicit Program class and Main method instead of top-level statements."
}
},
"tags": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,12 @@
"useHttps": true
}
],
"symbolInfo": [
{
"id": "UseProgramMain",
"isVisible": true
}
],
"excludeLaunchSettings": false,
"minFullFrameworkVersion": "4.6.1",
"disableHttpsSymbol": "NoHttps"
Expand Down
89 changes: 89 additions & 0 deletions src/content/Angular-CSharp/Program.Main.cs
Original file line number Diff line number Diff line change
@@ -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();
}
}
4 changes: 4 additions & 0 deletions src/content/React-CSharp/.template.config/dotnetcli.host.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@
"NoHttps": {
"longName": "no-https",
"shortName": ""
},
"UseProgramMain": {
"longName": "use-program-main",
"shortName": ""
}
},
"usageExamples": [
Expand Down
22 changes: 22 additions & 0 deletions src/content/React-CSharp/.template.config/template.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,21 @@
"wwwroot/**"
],
"modifiers": [
{
"condition": "(!UseProgramMain)",
"exclude": [
"Program.Main.cs"
]
},
{
"condition": "(UseProgramMain)",
"exclude": [
"Program.cs"
],
"rename": {
"Program.Main.cs": "Program.cs"
}
},
{
"condition": "(!IndividualLocalAuth)",
"exclude": [
Expand Down Expand Up @@ -267,6 +282,13 @@
"HostIdentifier": {
"type": "bind",
"binding": "HostIdentifier"
},
"UseProgramMain": {
"type": "parameter",
"datatype": "bool",
"defaultValue": "false",
"displayName": "Do not use top-level statements",
"description": "Whether to generate an explicit Program class and Main method instead of top-level statements."
}
},
"tags": {
Expand Down
6 changes: 6 additions & 0 deletions src/content/React-CSharp/.template.config/vs-2017.3.host.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,12 @@
"useHttps": true
}
],
"symbolInfo": [
{
"id": "UseProgramMain",
"isVisible": true
}
],
"excludeLaunchSettings": false,
"minFullFrameworkVersion": "4.6.1",
"disableHttpsSymbol": "NoHttps"
Expand Down
90 changes: 90 additions & 0 deletions src/content/React-CSharp/Program.Main.cs
Original file line number Diff line number Diff line change
@@ -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();
}
}

0 comments on commit 08a4e74

Please sign in to comment.