Global.asax.cs 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. using System.Collections.Generic;
  2. using System.Data.Entity.Core.Mapping;
  3. using System.Data.Entity.Core.Metadata.Edm;
  4. using System.Data.Entity.Infrastructure;
  5. using System.Linq;
  6. using System.Reflection;
  7. using System.Web;
  8. using System.Web.Mvc;
  9. using System.Web.Optimization;
  10. using System.Web.Routing;
  11. using Autofac;
  12. using Autofac.Integration.Mvc;
  13. using EMIS.Utility;
  14. using System.Resources;
  15. using EMIS.Web.Controllers;
  16. using System;
  17. using EMIS.Web.Controls.DataAnnotations;
  18. using Autofac.Integration.Wcf;
  19. using System.Globalization;
  20. using Senparc.CO2NET;
  21. using Senparc.CO2NET.RegisterServices;
  22. using Senparc.Weixin;
  23. using Senparc.Weixin.Entities;
  24. namespace EMIS.Web
  25. {
  26. // 注意: 有关启用 IIS6 或 IIS7 经典模式的说明,
  27. // 请访问 http://go.microsoft.com/?LinkId=9394801
  28. public class MvcApplication : System.Web.HttpApplication, IAutofacContainerProvider
  29. {
  30. static IContainer _container;
  31. public IContainer Container
  32. {
  33. get { return _container; }
  34. set { _container = value; }
  35. }
  36. protected void Application_Start()
  37. {
  38. if (DateTimeFormatInfo.CurrentInfo != null)
  39. {
  40. var type = DateTimeFormatInfo.CurrentInfo.GetType();
  41. var field = type.GetField("generalLongTimePattern", BindingFlags.NonPublic | BindingFlags.Instance);
  42. if (field != null)
  43. field.SetValue(DateTimeFormatInfo.CurrentInfo, "yyyy-MM-dd HH:mm:ss");
  44. }
  45. var viewEngines = ViewEngines.Engines;
  46. var webFormEngines = viewEngines.OfType<WebFormViewEngine>().FirstOrDefault();
  47. if (webFormEngines != null)
  48. {
  49. viewEngines.Remove(webFormEngines);
  50. }
  51. AreaRegistration.RegisterAllAreas();
  52. BundleTable.EnableOptimizations = true;
  53. //WebApiConfig.Register(GlobalConfiguration.Configuration);
  54. FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
  55. RouteConfig.RegisterRoutes(RouteTable.Routes);
  56. BundleConfig.RegisterBundles(BundleTable.Bundles);
  57. //AuthConfig.RegisterAuth();
  58. var builder = new ContainerBuilder();
  59. builder.RegisterModule<EMIS.CommonLogic.DALModule>();
  60. builder.RegisterModule<EMIS.CommonLogic.ServiceModule>();
  61. builder.RegisterControllers(Assembly.GetExecutingAssembly()).PropertiesAutowired();
  62. try
  63. {
  64. var _container = builder.Build();
  65. AutofacHelper.Container = _container;
  66. AutofacHostFactory.Container = _container;
  67. DependencyResolver.SetResolver(new Autofac.Integration.Mvc.AutofacDependencyResolver(_container));
  68. }
  69. catch (Exception)
  70. {
  71. throw;
  72. }
  73. //EF优化代码
  74. using (Entities.EMISNewContext dbContext = new Entities.EMISNewContext())
  75. {
  76. var objectContext = ((IObjectContextAdapter)dbContext).ObjectContext;
  77. var mappintCollection = (StorageMappingItemCollection)objectContext.MetadataWorkspace.GetItemCollection(DataSpace.CSSpace);
  78. mappintCollection.GenerateViews(new List<EdmSchemaError>());
  79. }
  80. //MVC视图验证映射
  81. ModelMetadataProviders.Current = new CustomerMetadataProvider();
  82. //ViewEngines.Engines.Clear();
  83. //ViewEngines.Engines.Add(new EcViewEngine());
  84. //设置全局 Debug 状态
  85. var isGLobalDebug = false;
  86. var senparcSetting = SenparcSetting.BuildFromWebConfig(isGLobalDebug);
  87. //CO2NET 全局注册,必须!!
  88. IRegisterService register = RegisterService.Start(senparcSetting)
  89. .UseSenparcGlobal(false, null);
  90. /* 微信配置开始
  91. * 建议按照以下顺序进行注册
  92. */
  93. //设置微信 Debug 状态
  94. var isWeixinDebug = false;
  95. var senparcWeixinSetting = SenparcWeixinSetting.BuildFromWebConfig(isWeixinDebug);
  96. //微信全局注册,必须!!
  97. register.UseSenparcWeixin(senparcWeixinSetting, senparcSetting);
  98. }
  99. //public sealed class EcViewEngine : RazorViewEngine
  100. //{
  101. // public EcViewEngine()
  102. // {
  103. // ViewLocationFormats = new[]
  104. // {
  105. // "~/Views/{1}/{0}.cshtml",//系统默认规则
  106. // "~/Views/Shared/{0}.cshtml",//系统默认规则
  107. // "~/Views/CalendarManage/{1}/{0}.cshtml",//我们的规则
  108. // "~/Views/EvaluationManage/{1}/{0}.cshtml",//我们的规则
  109. // };
  110. // }
  111. // public override ViewEngineResult FindView(ControllerContext controllerContext, string viewName, string masterName, bool useCache)
  112. // {
  113. // return base.FindView(controllerContext, viewName, masterName, useCache);
  114. // }
  115. //}
  116. ///// <summary>
  117. ///// 验证处理 IPrinceple
  118. ///// 将保存在票据中的信息转为当前的用户信息
  119. /////
  120. ///// </summary>
  121. ///// <param name="sender"></param>
  122. ///// <param name="e"></param>
  123. //protected void Application_AuthenticateRequest(Object sender, EventArgs e)
  124. //{
  125. // FormsAuthenticationHelper fahelper = new FormsAuthenticationHelper();
  126. // List<string> lurl = new List<string>();
  127. // lurl.Add("Content/");
  128. // lurl.Add("Doc/");
  129. // lurl.Add("Scripts/");
  130. // lurl.Add("Login/");
  131. // lurl.Add("UploadFile/");
  132. // var cookieName = EMIS.Utility.Const.LOCAL_SETTING_LOGIN_COOKIENAME;
  133. // fahelper.AuthenticateRequest(Context, cookieName, lurl);
  134. //}
  135. //void Application_BeginRequest(object sender, EventArgs e)
  136. //{
  137. // try
  138. // {
  139. // string session_param_name = "ASPSESSID";
  140. // string session_cookie_name = EMIS.Utility.Const.LOCAL_SETTING_LOGIN_COOKIENAME;
  141. // if (HttpContext.Current.Request.Form[session_param_name] != null)
  142. // {
  143. // UpdateCookie(session_cookie_name, HttpContext.Current.Request.Form[session_param_name]);
  144. // }
  145. // else if (HttpContext.Current.Request.QueryString[session_param_name] != null)
  146. // {
  147. // UpdateCookie(session_cookie_name, HttpContext.Current.Request.QueryString[session_param_name]);
  148. // }
  149. // }
  150. // catch
  151. // {
  152. // }
  153. // //此处是身份验证
  154. // try
  155. // {
  156. // string auth_param_name = "AUTHID";
  157. // string auth_cookie_name = FormsAuthentication.FormsCookieName;
  158. // if (HttpContext.Current.Request.Form[auth_param_name] != null)
  159. // {
  160. // UpdateCookie(auth_cookie_name, HttpContext.Current.Request.Form[auth_param_name]);
  161. // }
  162. // else if (HttpContext.Current.Request.QueryString[auth_param_name] != null)
  163. // {
  164. // UpdateCookie(auth_cookie_name, HttpContext.Current.Request.QueryString[auth_param_name]);
  165. // }
  166. // }
  167. // catch { }
  168. //}
  169. private void UpdateCookie(string cookie_name, string cookie_value)
  170. {
  171. HttpCookie cookie = HttpContext.Current.Request.Cookies.Get(cookie_name);
  172. if (null == cookie)
  173. {
  174. cookie = new HttpCookie(cookie_name);
  175. }
  176. cookie.Value = cookie_value;
  177. HttpContext.Current.Request.Cookies.Set(cookie);//重新设定请求中的cookie值,将服务器端的session值赋值给它
  178. }
  179. }
  180. }