Startup.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. using CZFW.CMS.Web.App_Code;
  2. using CZFW.Core;
  3. using CZFW.Framework;
  4. using CZFW.Framework.Model;
  5. using CZFW.MDB;
  6. using CZFW.MDB.Util;
  7. using log4net;
  8. using log4net.Config;
  9. using log4net.Repository;
  10. using Microsoft.AspNetCore.Builder;
  11. using Microsoft.AspNetCore.Hosting;
  12. using Microsoft.AspNetCore.Http.Features;
  13. using Microsoft.EntityFrameworkCore;
  14. using Microsoft.Extensions.Configuration;
  15. using Microsoft.Extensions.DependencyInjection;
  16. using Microsoft.Extensions.Logging;
  17. using System;
  18. using System.IO;
  19. using System.Text.Encodings.Web;
  20. using System.Text.Unicode;
  21. namespace CZFW.CMS.Web
  22. {
  23. public class Startup
  24. {
  25. public static ILoggerRepository repository { get; set; }
  26. public Startup(IConfiguration configuration)
  27. {
  28. Configuration = configuration;
  29. ConfigHelper.Configs = configuration;
  30. repository = LogManager.CreateRepository("NetCoreRepository");
  31. XmlConfigurator.Configure(repository, new FileInfo("log4net.config"));
  32. App_Code.InitRepository.loggerRepository = repository;
  33. }
  34. public IConfiguration Configuration { get; }
  35. // This method gets called by the runtime. Use this method to add services to the container.
  36. public void ConfigureServices(IServiceCollection services)
  37. {
  38. services.ConfigureFrameworkServices();
  39. services.ConfigureMDBServices();
  40. services.AddMemoryCache();
  41. services.AddSingleton(HtmlEncoder.Create(UnicodeRanges.All));
  42. services.Configure<Microsoft.AspNetCore.Mvc.Razor.RazorViewEngineOptions>(options =>
  43. {
  44. options.ViewLocationExpanders.Add(new CZViewLocationExpander());
  45. });
  46. //services.AddDbContext<CZDbContext>(options =>
  47. //{
  48. // options.UseMySql(ConfigHelper.GetValue<string>("ConnectionStrings:MySql"),
  49. // mysqlOptions =>
  50. // {
  51. // //mysqlOptions.ServerVersion("5.1");
  52. // }
  53. // );
  54. //});
  55. services.AddSession(options =>
  56. {
  57. options.IdleTimeout = TimeSpan.FromMinutes(180);
  58. });
  59. services.AddMvc(o => o.Filters.Add(typeof(GlobalExceptions)));
  60. services.Configure<FormOptions>(options =>
  61. {
  62. options.MultipartBodyLengthLimit = 60000000;
  63. });
  64. services.AddResponseCaching();
  65. CZHttpContext.ServiceProvider = services.BuildServiceProvider();
  66. }
  67. // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
  68. public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory logger)
  69. {
  70. app.UseSession();
  71. //if (env.IsDevelopment())
  72. //{
  73. // app.UseDeveloperExceptionPage();
  74. // app.UseBrowserLink();
  75. //}
  76. //else
  77. //{
  78. // app.UseExceptionHandler("/Home/Error");
  79. //}
  80. app.UseStatusCodePagesWithReExecute("/Home/CodePage/{0}");
  81. app.UseResponseCaching();
  82. logger.AddLog4Net();
  83. logger.AddEventSourceLogger();
  84. app.UseStaticFiles();
  85. app.UseMvc(routes =>
  86. {
  87. var langs = ConfigHelper.GetValue("Multilanguage:Languages").Split(',', System.StringSplitOptions.RemoveEmptyEntries);
  88. foreach (var lan in langs)
  89. {
  90. var lang = lan.Trim();
  91. routes.MapRoute(
  92. name: "error" + lang, template: $"{lang}/home/Error",
  93. defaults: new { controller = "Home", action = "Error", theme = lang });
  94. routes.MapRoute(
  95. name: "CodePage" + lang, template: $"{lang}/home/codepage/{{code?}}",
  96. defaults: new { controller = "Home", action = "CodePage", theme = lang });
  97. routes.MapRoute(
  98. name: "home" + lang, template: $"{lang}",
  99. defaults: new { controller = "Home", action = "Index", theme = lang });
  100. routes.MapRoute(
  101. name: "about" + lang, template: $"{lang}/aboutus",
  102. defaults: new { controller = "Home", action = "About", theme = lang });
  103. routes.MapRoute(
  104. name: "contact" + lang, template: $"{lang}/contact",
  105. defaults: new { controller = "Home", action = "Contact", theme = lang });
  106. routes.MapRoute(
  107. name: "list" + lang, template: $"{lang}/{{pageName}}/{{action}}.html",
  108. defaults: new { controller = "Data", action = "List", theme = lang });
  109. routes.MapRoute(
  110. name: "homeController" + lang, template: $"{lang}/home/{{action}}/{{id?}}",
  111. defaults: new { controller = "Home", action = "Index", theme = lang });
  112. routes.MapRoute(
  113. name: "detail" + lang, template: $"{lang}/{{pageName}}/{{action}}/{{id}}.html",
  114. defaults: new { controller = "Data", action = "Detail", theme = lang });
  115. routes.MapRoute(
  116. name: "data" + lang, template: $"{lang}/{{pageName}}/{{action}}/{{id?}}",
  117. defaults: new { controller = "Data", action = "List", theme = lang });
  118. routes.MapRoute(
  119. name: "default" + lang,
  120. template: $"{lang}/{{controller=Home}}/{{action=Index}}/{{id?}}");
  121. }
  122. var defaultLang = ConfigHelper.GetValue("Multilanguage:Default");
  123. routes.MapRoute(
  124. name: "error", template: $"home/Error",
  125. defaults: new { controller = "Home", action = "Error", theme = "default" });
  126. routes.MapRoute(
  127. name: "CodePage", template: $"home/codepage/{{code?}}",
  128. defaults: new { controller = "Home", action = "CodePage", theme = defaultLang });
  129. routes.MapRoute(
  130. name: "home", template: $"",
  131. defaults: new { controller = "Home", action = "Index", theme = defaultLang });
  132. routes.MapRoute(
  133. name: "about", template: $"aboutus",
  134. defaults: new { controller = "Home", action = "About", theme = defaultLang });
  135. routes.MapRoute(
  136. name: "contact", template: $"contact",
  137. defaults: new { controller = "Home", action = "Contact", theme = defaultLang });
  138. routes.MapRoute(
  139. name: "resource", template: $"{Constants.RESOURCE_FILE_URL_PREFIX}/{{siteName}}/{{fileType}}/{{fileName}}",
  140. defaults: new { controller = "Resource", action = "Load" });
  141. routes.MapRoute(
  142. name: "content", template: $"{Constants.CONTENT_FILE_URL_PREFIX}/{{tplId}}/{{theme}}/{{folder}}/{{typeFolder}}/{{fileName}}",
  143. defaults: new { controller = "Resource", action = "Content" });
  144. routes.MapRoute(
  145. name: "list", template: "{pageName}/{action}.html",
  146. defaults: new { controller = "Data", action = "List" });
  147. routes.MapRoute(
  148. name: "homeController", template: $"home/{{action}}/{{id?}}",
  149. defaults: new { controller = "Home", action = "Index", theme = defaultLang });
  150. routes.MapRoute(
  151. name: "detail", template: "{pageName}/{action}/{id}.html",
  152. defaults: new { controller = "Data", action = "Detail" });
  153. routes.MapRoute(
  154. name: "data", template: $"{{pageName}}/{{action}}/{{id?}}",
  155. defaults: new { controller = "Data", action = "List", theme = defaultLang });
  156. routes.MapRoute(
  157. name: "default",
  158. template: $"{{controller=Home}}/{{action=Index}}/{{id?}}");
  159. //routes.MapRoute(name: "download", template: "en/down.html", defaults: new { controller = "Home", action = "Download" });
  160. //routes.MapRoute(name: "english", template: "en/{name?}", defaults: new { controller = "Home", action = "English" }, constraints: new { name = @"([a-z]*/)*([a-z0-9]+).html" });
  161. //routes.MapRoute(
  162. // name: "home", template: "",
  163. // defaults: new { controller = "Home", action = "Index" });
  164. //routes.MapRoute(
  165. // name: "about", template: "aboutus",
  166. // defaults: new { controller = "Home", action = "About" });
  167. //routes.MapRoute(
  168. // name: "contact", template: "contact",
  169. // defaults: new { controller = "Home", action = "Contact" });
  170. //routes.MapRoute(
  171. // name: "resource", template: $"{Constants.RESOURCE_FILE_URL_PREFIX}/{{siteId}}/{{fileType}}/{{fileName}}",
  172. // defaults: new { controller = "Resource", action = "Load" });
  173. //routes.MapRoute(
  174. // name: "content", template: $"{Constants.CONTENT_FILE_URL_PREFIX}/{{tplId}}/{{theme}}/{{folder}}/{{typeFolder}}/{{fileName}}",
  175. // defaults: new { controller = "Resource", action = "Content" });
  176. //routes.MapRoute(
  177. // name: "list", template: "{pageName}/{action}.html",
  178. // defaults: new { controller = "Data", action = "List" });
  179. //routes.MapRoute(
  180. // name: "homeController", template: "home/{action}/{id?}",
  181. // defaults: new { controller = "Home", action = "Index" });
  182. //routes.MapRoute(
  183. // name: "detail", template: "{pageName}/{action}/{id}.html",
  184. // defaults: new { controller = "Data", action = "Detail" });
  185. //routes.MapRoute(
  186. // name: "default", template: "{pageName}/{action}/{id?}",
  187. // defaults: new { controller = "Data", action = "List" });
  188. });
  189. }
  190. }
  191. }