-
Notifications
You must be signed in to change notification settings - Fork 30
Expand file tree
/
Copy pathStartup.cs
More file actions
286 lines (204 loc) · 10.6 KB
/
Startup.cs
File metadata and controls
286 lines (204 loc) · 10.6 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
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
using cloudscribe.Web.Localization;
using cloudscribe.Web.SiteMap;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Localization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Razor;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Localization;
using Microsoft.Extensions.Options;
using System.Globalization;
using System.Linq;
namespace NavigationDemo.Web
{
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddScoped<ISiteMapNodeService, NavigationTreeSiteMapNodeService>();
services.AddCloudscribeNavigation(Configuration.GetSection("NavigationOptions"));
services.AddScoped<cloudscribe.Web.Navigation.INavigationTreeBuilder, cloudscribe.Web.Navigation.JsonNavigationTreeBuilder>();
// this was an experiment for https://github.com/cloudscribe/cloudscribe.Web.Navigation/issues/71
//services.AddScoped<cloudscribe.Web.Navigation.INodeUrlPrefixProvider, CustomUrlPrefixProvider>();
services.Configure<GlobalResourceOptions>(Configuration.GetSection("GlobalResourceOptions"));
services.AddSingleton<IStringLocalizerFactory, GlobalResourceManagerStringLocalizerFactory>();
services.AddLocalization(options => options.ResourcesPath = "Resources");
services.AddRouting(options =>
{
options.LowercaseUrls = true;
});
services.Configure<MvcOptions>(options =>
{
// options.InputFormatters.Add(new Xm)
options.CacheProfiles.Add("SiteMapCacheProfile",
new CacheProfile
{
Duration = 100
});
});
services.AddMvc()
.AddViewLocalization(LanguageViewLocationExpanderFormat.Suffix)
.AddDataAnnotationsLocalization()
.AddRazorOptions(options => {})
;
services.AddControllersWithViews();
services.AddRazorPages();
services.AddAuthentication(options =>
{
options.DefaultAuthenticateScheme = "application";
options.DefaultChallengeScheme = "application";
})
.AddCookie("application", options =>
{
options.LoginPath = new PathString("/FakeAccount/Index");
});
var supportedCultures = new[]
{
new CultureInfo("en-US"),
new CultureInfo("es-ES"),
new CultureInfo("fr-FR"),
new CultureInfo("de-DE"),
new CultureInfo("hi-IN"), //Hindi
new CultureInfo("ru-RU"),
new CultureInfo("zh-Hans"), //Chinese Simplified
new CultureInfo("zh-Hant"), //Chinese Traditional
};
var routeSegmentLocalizationProvider = new FirstUrlSegmentRequestCultureProvider(supportedCultures.ToList());
services.Configure<RequestLocalizationOptions>(options =>
{
// State what the default culture for your application is. This will be used if no specific culture
// can be determined for a given request.
options.DefaultRequestCulture = new RequestCulture(culture: "en-US", uiCulture: "en-US");
// You must explicitly state which cultures your application supports.
// These are the cultures the app supports for formatting numbers, dates, etc.
options.SupportedCultures = supportedCultures;
// These are the cultures the app supports for UI strings, i.e. we have localized resources for.
options.SupportedUICultures = supportedCultures;
// You can change which providers are configured to determine the culture for requests, or even add a custom
// provider with your own logic. The providers will be asked in order to provide a culture for each request,
// and the first to provide a non-null result that is in the configured supported cultures list will be used.
// By default, the following built-in providers are configured:
// - QueryStringRequestCultureProvider, sets culture via "culture" and "ui-culture" query string values, useful for testing
// - CookieRequestCultureProvider, sets culture via "ASPNET_CULTURE" cookie
// - AcceptLanguageHeaderRequestCultureProvider, sets culture via the "Accept-Language" request header
//options.RequestCultureProviders.Insert(0, new CustomRequestCultureProvider(async context =>
//{
// // My custom request culture logic
// return new ProviderCultureResult("en");
//}));
options.RequestCultureProviders.Insert(0, routeSegmentLocalizationProvider);
});
services.AddAuthorization(options =>
{
options.AddPolicy(
"AdminsPolicy",
authBuilder =>
{
authBuilder.RequireRole("Admins");
});
options.AddPolicy(
"MembersPolicy",
authBuilder =>
{
authBuilder.RequireRole("Admins", "Members");
});
});
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(
IApplicationBuilder app,
IWebHostEnvironment env,
IOptions<RequestLocalizationOptions> locOptions
)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/Home/Error");
}
//https://docs.microsoft.com/en-us/aspnet/core/fundamentals/localization
//https://msdn.microsoft.com/en-us/library/ee825488(v=cs.20).aspx
//var locOptions = app.ApplicationServices.GetService<IOptions<RequestLocalizationOptions>>();
app.UseRequestLocalization(locOptions.Value);
app.UseStaticFiles();
app.UseRouting();
app.UseAuthentication();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
//endpoints.MapControllerRoute(
// name: "default",
// pattern: "{controller=Home}/{action=Index}/{id?}");
endpoints.MapControllerRoute(
name: "areaRoute-localized",
pattern: "{culture}/{area:exists}/{controller}/{action}/{id?}",
defaults: new { action = "Index" },
constraints: new { culture = new CultureSegmentRouteConstraint() }
);
endpoints.MapControllerRoute("areaRoute", "{area:exists}/{controller=Roswell}/{action=Index}/{id?}");
endpoints.MapControllerRoute(
name: "OverviewIndex",
pattern: "overview"
, defaults: new { controller = "Overview", action = "Index" }
);
endpoints.MapControllerRoute(
name: "OverviewWhatever",
pattern: "whatever/overview"
, defaults: new { controller = "Whatever", action = "Overview" }
);
endpoints.MapControllerRoute(
name: "default-localized",
pattern: "{culture}/{controller}/{action}/{id?}",
defaults: new { controller = "Home", action = "Index" },
constraints: new { culture = new CultureSegmentRouteConstraint() }
);
endpoints.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
endpoints.MapRazorPages();
});
//app.UseMvc(routes =>
//{
// routes.MapRoute(
// name: "areaRoute-localized",
// template:"{culture}/{area:exists}/{controller}/{action}/{id?}",
// defaults: new { action = "Index" },
// constraints: new { culture = new CultureSegmentRouteConstraint() }
// );
// routes.MapRoute("areaRoute", "{area:exists}/{controller=Roswell}/{action=Index}/{id?}");
// routes.MapRoute(
// name: "OverviewIndex",
// template: "overview"
// , defaults: new { controller = "Overview", action = "Index" }
// );
// routes.MapRoute(
// name: "OverviewWhatever",
// template: "whatever/overview"
// , defaults: new { controller = "Whatever", action = "Overview" }
// );
// routes.MapRoute(
// name: "default-localized",
// template: "{culture}/{controller}/{action}/{id?}",
// defaults: new { controller= "Home", action = "Index" },
// constraints: new { culture = new CultureSegmentRouteConstraint() }
// );
// routes.MapRoute(
// name: "default",
// template: "{controller=Home}/{action=Index}/{id?}");
//});
}
}
}