-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathProgram.cs
212 lines (156 loc) · 6.54 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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
using Arebis.Core.AspNet.Mvc.Localization;
using Arebis.Core.Localization;
using Arebis.Core.Services.Interfaces;
using Arebis.Core.Services.Translation;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Localization;
using Microsoft.AspNetCore.Localization.Routing;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Options;
using MyMvcApp.Data;
using MyMvcApp.Data.Content;
using MyMvcApp.Localize;
using MyMvcApp.Logging;
using System.Collections.Generic;
using System.Globalization;
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlServer(builder.Configuration.GetConnectionString("DefaultConnection"),
o => o.UseQuerySplittingBehavior(QuerySplittingBehavior.SplitQuery)));
builder.Services.AddDatabaseDeveloperPageExceptionFilter();
#region Content
builder.Services.AddDbContext<ContentDbContext>(options =>
options.UseSqlServer(builder.Configuration.GetConnectionString("DefaultConnection"),
o => o.UseQuerySplittingBehavior(QuerySplittingBehavior.SplitQuery)));
#endregion
#region Localization
builder.Services.AddDbContext<MyMvcApp.Data.Localize.LocalizeDbContext>(/*contextLifetime: ServiceLifetime.Transient, optionsLifetime: ServiceLifetime.Singleton, */optionsAction: options =>
options.UseSqlServer(builder.Configuration.GetConnectionString("DefaultConnection"),
o => o.UseQuerySplittingBehavior(QuerySplittingBehavior.SplitQuery)));
builder.Services.AddLocalizationFromSource(builder.Configuration, options => {
options.AllowLocalizeFormat = false;
options.CacheFileName = "LocalizationCache.json";
options.Domains = new string[] { "Base", "Asp.NET", "Test", "Website" };
// options.Domains = new string[] { "Test", "TestApp" };
//options.RouteDataStringKey = "culture";
//options.UIRouteDataStringKey = "uiculture";
options.UseOnlyReviewedLocalizationValues = false;
});
builder.Services.AddModelBindingLocalizationFromSource();
// Install optional global filters, or add them to individual controllers or actions:
builder.Services.AddControllers(config =>
{
config.Filters.Add<ModelStateLocalizationFilter>();
});
builder.Services.AddTransient<ILocalizationSource, DbContextLocalizationSource>();
//builder.Services.AddTransient<ITranslationService, DeepLTranslationService>();
builder.Services.AddTransient<ITranslationService, GoogleTranslationService>();
//builder.Services.AddTransient<ITranslationService, BingTranslationService>();
#endregion
builder.Services.AddDefaultIdentity<IdentityUser>(options => options.SignIn.RequireConfirmedAccount = true)
.AddRoles<IdentityRole>()
.AddEntityFrameworkStores<ApplicationDbContext>();
builder.Services.AddControllersWithViews()
// .AddDataAnnotationsLocalization()
#region Localization
.AddDataAnnotationsLocalizationFromSource()
#endregion
;
#region Request Localization
builder.Services.Configure<RequestLocalizationOptions>(options =>
{
List<CultureInfo> supportedCultures = new List<CultureInfo>
{
new CultureInfo("en"),
new CultureInfo("nl"),
new CultureInfo("fr"),
new CultureInfo("de"),
};
options.DefaultRequestCulture = new RequestCulture("en");
options.SupportedCultures = supportedCultures;
options.SupportedUICultures = supportedCultures;
options.RequestCultureProviders.Clear();
options.RequestCultureProviders.Add(new QueryStringRequestCultureProvider());
options.RequestCultureProviders.Add(new RouteDataRequestCultureProvider());
options.RequestCultureProviders.Add(new AcceptLanguageHeaderRequestCultureProvider());
//options.RequestCultureProviders.Add(new CustomRequestCultureProvider(async context =>
//{
// //Write your code here
// return new ProviderCultureResult("nl-BE");
//}));
});
#endregion
#region Tasks
builder.Services.AddDbContext<MyMvcApp.Data.Tasks.ScheduledTasksDbContext>(options =>
options.UseSqlServer(builder.Configuration.GetConnectionString("DefaultConnection"),
o => o.UseQuerySplittingBehavior(QuerySplittingBehavior.SplitQuery)));
builder.Services.AddHostedService<MyMvcApp.Tasks.TaskScheduler>();
#endregion
#region Logging
builder.Services.AddDbContext<MyMvcApp.Data.Logging.LoggingDbContext>(options =>
options.UseSqlServer(builder.Configuration.GetConnectionString("DefaultConnection"),
o => o.UseQuerySplittingBehavior(QuerySplittingBehavior.SplitQuery)));
builder.Services.AddScoped<MyMvcApp.Logging.RequestLogger>();
#endregion
#region Output caching
// Is not set by default; need to call AddOuptutCache() on services, and UseOutputCache on app (after calling UseRouting):
builder.Services.AddOutputCache();
#endregion
var app = builder.Build();
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseDeveloperExceptionPage();
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();
}
#region Logging
app.UseArebisRequestLog()
.ApplyDoNotLogRule()
.LogSlowRequests()
.LogExceptions()
.LogNotFounds();
#endregion
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
#region Output caching
// Is not set by default; need to call AddOuptutCache() on services, and UseOutputCache on app (after calling UseRouting):
app.UseOutputCache();
#endregion
app.UseAuthentication();
app.UseAuthorization();
#region Localization
app.UseLocalizationFromSource();
#endregion
#region Request Localization
var rloptions = app.Services.GetService<IOptions<RequestLocalizationOptions>>();
app.UseRequestLocalization(rloptions!.Value);
#endregion
app.MapControllerRoute(
name: "area",
pattern: "{culture:regex(^[a-z][a-z](\\-[A-Z][A-Z]){{0,1}}$)=en}/{area:exists}/{controller=Home}/{action=Index}/{id?}");
app.MapControllerRoute(
name: "default",
pattern: "{culture:regex(^[a-z][a-z](\\-[A-Z][A-Z]){{0,1}}$)=en}/{controller=Home}/{action=Index}/{id?}");
app.MapRazorPages();
#region Content
app.MapControllerRoute(
name: "content",
//pattern: "{**path}",
pattern: "{culture:regex(^[a-z][a-z](\\-[A-Z][A-Z]){{0,1}}$)=en}/{**path}",
defaults: new { controller = "Content", action = "Render" }
);
#endregion
app.MapFallbackToFile("404.html");
app.Run();