-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
106 lines (81 loc) · 3.95 KB
/
Program.cs
File metadata and controls
106 lines (81 loc) · 3.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
using System.Diagnostics;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.InMemory;
using Microsoft.EntityFrameworkCore.SqlServer;
using TodoWebApp.Logging;
using TodoWebApp.Models;
var builder = WebApplication.CreateBuilder(args);
#region [Process any cmd line args]
// grab "--port=1234" from the raw args
var portArg = args.FirstOrDefault(a => a.StartsWith("--port=", StringComparison.OrdinalIgnoreCase))?.Split('=', 2)[1];
if (!string.IsNullOrEmpty(portArg) && int.TryParse(portArg, out var port))
{
// bind HTTP on the chosen port
builder.WebHost.UseUrls($"http://localhost:{port}");
}
//else // fallback or could throw
//{
// builder.WebHost.UseUrls("http://localhost:5050");
//}
#endregion
#region [Wire-up Logging]
var logFile = Path.Combine(builder.Environment.ContentRootPath, "Logs", "WebApp.log");
Debug.WriteLine($"[INFO] Log file path is here '{logFile}'"); // D:\source\repos\ASP.NET\TodoWebApp\Logs\WebApp.log
//var logFilewww = Path.Combine(builder.Environment.WebRootPath, "Logs", "WebApp.log");
//Debug.WriteLine($"[INFO] wwwroot log file path is here '{logFilewww}'"); // D:\source\repos\ASP.NET\TodoWebApp\wwwroot\Logs\WebApp.log
builder.Logging.ClearProviders(); // clear default providers (optional, but keeps console/debug separate)
// Filter out the hosting-lifetime noise
//builder.Logging.AddFilter("Microsoft.Hosting.Lifetime", LogLevel.None);
//builder.Logging.AddFilter("Microsoft.EntityFrameworkCore.Database.Command", LogLevel.None);
// (Optional) only log your own namespace at Information
builder.Logging.AddFilter("TodoWebApp", LogLevel.Information);
builder.Logging.AddConsole(); // register Microsoft's console output
builder.Logging.AddDebug(); // register Microsoft's debug output
builder.Logging.AddFile(logFile);
/* [Also can be done via "appsettings.json"]
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.Hosting.Lifetime": "None",
"TodoWebApp": "Information"
}
}
}
*/
#endregion
#region [Register EF Core with InMemory DB]
// Uncomment the following line to use an in-memory database for testing purposes (needs Microsoft.EntityFrameworkCore.InMemory package)
//builder.Services.AddDbContext<AppDbContext>(opts => opts.UseInMemoryDatabase("TodoList"));
#endregion
#region [Register EF Core with SQL Server]
var connectionString = builder.Configuration.GetConnectionString("DefaultConnection");
builder.Services.AddDbContext<AppDbContext>(opts => opts.UseSqlServer(connectionString));
// Alternatively, you can use the following line to register the DbContext with a connection string from appsettings.json
//builder.Services.AddDbContext<AppDbContext>(opt => opt.UseSqlServer(builder.Configuration["ConnectionStrings:DefaultConnection"]));
#endregion
// Add services to the container.
builder.Services.AddControllersWithViews();
var app = builder.Build();
// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())
{
app.UseExceptionHandler("/Home/Error");
// 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();
app.UseRouting();
app.UseAuthorization();
app.MapStaticAssets();
// Middleware pipeline
app.UseStaticFiles(); // serves "wwwroot/*"
//app.UseStaticFiles(new StaticFileOptions { RequestPath = "/images" }); // serves "wwwroot/images/*"
// This is needed to serve static files from the wwwroot folder when not running from the VisualStudio IDE.
Microsoft.AspNetCore.Hosting.StaticWebAssets.StaticWebAssetsLoader
.UseStaticWebAssets(app.Environment, builder.Configuration);
//app.MapControllerRoute(name: "default", pattern: "{controller=Home}/{action=Index}/{id?}").WithStaticAssets();
app.MapControllerRoute(name: "default", pattern: "{controller=TodoItems}/{action=Index}/{id?}");
// Let's go!
app.Run();