-
Notifications
You must be signed in to change notification settings - Fork 214
/
Copy pathProgram.cs
125 lines (97 loc) · 3.7 KB
/
Program.cs
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#region Import namespaces
using Microsoft.AspNetCore.Identity; // To use IdentityUser.
using Microsoft.EntityFrameworkCore; // To use UseSqlServer method.
using Northwind.Mvc.Data; // To use ApplicationDbContext.
using Northwind.EntityModels; // To use AddNorthwindContext method.
using System.Net.Http.Headers; // To use MediaTypeWithQualityHeaderValue.
using System.Net; // To use HttpVersion.
#endregion
#region Configure the host web server including services
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
var connectionString = builder.Configuration
.GetConnectionString("DefaultConnection") ??
throw new InvalidOperationException(
"Connection string 'DefaultConnection' not found.");
builder.Services.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlServer(connectionString)); // Or UseSqlite.
builder.Services.AddDatabaseDeveloperPageExceptionFilter();
builder.Services.AddDefaultIdentity<IdentityUser>(options =>
options.SignIn.RequireConfirmedAccount = true)
.AddRoles<IdentityRole>() // Enable role management.
.AddEntityFrameworkStores<ApplicationDbContext>();
builder.Services.AddControllersWithViews();
// If you are using SQLite, default is "..\Northwind.db".
builder.Services.AddNorthwindContext();
/*
// If you are using SQL Server.
string? sqlServerConnection = builder.Configuration
.GetConnectionString("NorthwindConnection");
if (sqlServerConnection is null)
{
Console.WriteLine("SQL Server database connection string is missing!");
}
else
{
// If you are using SQL Server authentication then disable
// Windows Integrated authentication and set user and password.
Microsoft.Data.SqlClient.SqlConnectionStringBuilder sql =
new(sqlServerConnection);
sql.IntegratedSecurity = false;
sql.UserID = Environment.GetEnvironmentVariable("MY_SQL_USR");
sql.Password = Environment.GetEnvironmentVariable("MY_SQL_PWD");
builder.Services.AddNorthwindContext(sql.ConnectionString);
}
*/
builder.Services.AddOutputCache(options =>
{
options.DefaultExpirationTimeSpan = TimeSpan.FromSeconds(20);
options.AddPolicy("views", p => p.SetVaryByQuery("alertstyle"));
});
builder.Services.AddHttpClient(name: "Northwind.WebApi",
configureClient: options =>
{
options.DefaultRequestVersion = HttpVersion.Version30;
options.DefaultVersionPolicy = HttpVersionPolicy.RequestVersionExact;
options.BaseAddress = new Uri("https://localhost:5151/");
options.DefaultRequestHeaders.Accept.Add(
new MediaTypeWithQualityHeaderValue(
mediaType: "application/json", quality: 1.0));
});
builder.Services.AddHttpClient(name: "Northwind.MinimalApi",
configureClient: options =>
{
options.BaseAddress = new Uri("http://localhost:5152/");
options.DefaultRequestHeaders.Accept.Add(
new MediaTypeWithQualityHeaderValue(
"application/json", 1.0));
});
var app = builder.Build();
#endregion
#region Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseMigrationsEndPoint();
}
else
{
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.UseStaticFiles();
app.UseRouting();
app.UseAuthorization();
app.UseOutputCache();
app.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
// .CacheOutput(policyName: "views");
app.MapRazorPages();
app.MapGet("/notcached", () => DateTime.Now.ToString());
app.MapGet("/cached", () => DateTime.Now.ToString()).CacheOutput();
#endregion
#region Start the host web server listening for HTTP requests.
app.Run(); // This is a blocking call.
#endregion