-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathProgram.cs
114 lines (93 loc) · 2.99 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
using Base.Enums;
using Base.Interfaces;
using Base.Models;
using Base.Services;
using BaseWeb.Services;
using DbAdm.Services;
using Microsoft.AspNetCore.Mvc.Razor;
using System.Data.Common;
using System.Data.SqlClient;
var builder = WebApplication.CreateBuilder(args);
var services = builder.Services;
// Add services to the container.
//builder.Services.AddControllersWithViews();
#region set services
//1.config MVC
services.AddControllersWithViews()
//view Localization
.AddViewLocalization(LanguageViewLocationExpanderFormat.Suffix)
//use pascal for newtonSoft json
.AddNewtonsoftJson(opts => { opts.UseMemberCasing(); })
//use pascal for MVC json
.AddJsonOptions(opts => { opts.JsonSerializerOptions.PropertyNamingPolicy = null; });
//2.set Resources path
services.AddLocalization(opts => opts.ResourcesPath = "Resources");
//3.http context
services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
//4.user info for base component
services.AddSingleton<IBaseUserSvc, MyBaseUserService>();
//5.ado.net for mssql
services.AddTransient<DbConnection, SqlConnection>();
services.AddTransient<DbCommand, SqlCommand>();
//6.appSettings "FunConfig" section -> _Fun.Config
var config = new ConfigDto();
builder.Configuration.GetSection("FunConfig").Bind(config);
_Fun.Config = config;
//cache server
//services.AddDistributedMemoryCache(); //AddDistributedRedisCache is old
services.AddMemoryCache();
//services.AddStackExchangeRedisCache(opts => { opts.Configuration = config.Redis; });
services.AddSingleton<ICacheSvc, CacheMemSvc>();
/*
//7.session (memory cache)
services.AddDistributedMemoryCache();
//services.AddStackExchangeRedisCache(opts => { opts.Configuration = "127.0.0.1:6379"; });
services.AddSession(opts =>
{
opts.Cookie.HttpOnly = true;
opts.Cookie.IsEssential = true;
opts.IdleTimeout = TimeSpan.FromMinutes(60);
});
*/
//cors
string[] origins = _Fun.Config.AllowOrigins.Split(',');
services.AddCors(opts =>
{
opts.AddDefaultPolicy(a =>
{
a.WithOrigins(origins);
a.AllowAnyHeader();
a.AllowAnyMethod();
a.AllowCredentials();
});
});
#endregion
var app = builder.Build();
//initial & set locale
var isDev = app.Environment.IsDevelopment();
_Fun.Init(isDev, app.Services, DbTypeEnum.MSSql, AuthTypeEnum.Row);
await _Locale.SetCultureA(_Fun.Config.Locale);
// Configure the HTTP request pipeline.
if (isDev)
{
//app.UseMigrationsEndPoint();
app.UseDeveloperExceptionPage();
//app.UseExceptionHandler("/Home/Error");
}
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.UseCors(); //加上後會套用到全域
app.UseAuthentication(); //認証
app.UseAuthorization(); //授權
//app.UseSession();
app.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Login}/{id?}");
app.Run();