Global.asax.cs 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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 EMISOnline.Utility;
  14. using System.Resources;
  15. namespace EMISOnline.Web
  16. {
  17. // 注意: 有关启用 IIS6 或 IIS7 经典模式的说明,
  18. // 请访问 http://go.microsoft.com/?LinkId=9394801
  19. public class MvcApplication : System.Web.HttpApplication, IAutofacContainerProvider
  20. {
  21. static IContainer _container;
  22. public IContainer Container
  23. {
  24. get { return _container; }
  25. set { _container = value; }
  26. }
  27. protected void Application_Start()
  28. {
  29. var viewEngines = ViewEngines.Engines;
  30. var webFormEngines = viewEngines.OfType<WebFormViewEngine>().FirstOrDefault();
  31. if (webFormEngines != null)
  32. {
  33. viewEngines.Remove(webFormEngines);
  34. }
  35. AreaRegistration.RegisterAllAreas();
  36. BundleTable.EnableOptimizations = true;
  37. //WebApiConfig.Register(GlobalConfiguration.Configuration);
  38. FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
  39. RouteConfig.RegisterRoutes(RouteTable.Routes);
  40. BundleConfig.RegisterBundles(BundleTable.Bundles);
  41. //AuthConfig.RegisterAuth();
  42. var builder = new ContainerBuilder();
  43. builder.RegisterModule<EMISOnline.CommonLogic.DALModule>();
  44. builder.RegisterModule<EMISOnline.CommonLogic.ServiceModule>();
  45. builder.RegisterControllers(Assembly.GetExecutingAssembly()).PropertiesAutowired();
  46. var _container = builder.Build();
  47. AutofacHelper.Container = _container;
  48. DependencyResolver.SetResolver(new Autofac.Integration.Mvc.AutofacDependencyResolver(_container));
  49. //EF优化代码
  50. using (Entities.EMISOnlineContextContainer dbContext = new Entities.EMISOnlineContextContainer())
  51. {
  52. var objectContext = ((IObjectContextAdapter)dbContext).ObjectContext;
  53. var mappintCollection = (StorageMappingItemCollection)objectContext.MetadataWorkspace.GetItemCollection(DataSpace.CSSpace);
  54. mappintCollection.GenerateViews(new List<EdmSchemaError>());
  55. }
  56. //ViewEngines.Engines.Clear();
  57. //ViewEngines.Engines.Add(new EcViewEngine());
  58. }
  59. //public sealed class EcViewEngine : RazorViewEngine
  60. //{
  61. // public EcViewEngine()
  62. // {
  63. // ViewLocationFormats = new[]
  64. // {
  65. // "~/Views/{1}/{0}.cshtml",//系统默认规则
  66. // "~/Views/Shared/{0}.cshtml",//系统默认规则
  67. // "~/Views/CalendarManage/{1}/{0}.cshtml",//我们的规则
  68. // "~/Views/EvaluationManage/{1}/{0}.cshtml",//我们的规则
  69. // };
  70. // }
  71. // public override ViewEngineResult FindView(ControllerContext controllerContext, string viewName, string masterName, bool useCache)
  72. // {
  73. // return base.FindView(controllerContext, viewName, masterName, useCache);
  74. // }
  75. //}
  76. ///// <summary>
  77. ///// 验证处理 IPrinceple
  78. ///// 将保存在票据中的信息转为当前的用户信息
  79. /////
  80. ///// </summary>
  81. ///// <param name="sender"></param>
  82. ///// <param name="e"></param>
  83. //protected void Application_AuthenticateRequest(Object sender, EventArgs e)
  84. //{
  85. // FormsAuthenticationHelper fahelper = new FormsAuthenticationHelper();
  86. // List<string> lurl = new List<string>();
  87. // lurl.Add("Content/");
  88. // lurl.Add("Doc/");
  89. // lurl.Add("Scripts/");
  90. // lurl.Add("Login/");
  91. // lurl.Add("UploadFile/");
  92. // var cookieName = EMIS.Utility.Const.LOCAL_SETTING_LOGIN_COOKIENAME;
  93. // fahelper.AuthenticateRequest(Context, cookieName, lurl);
  94. //}
  95. //void Application_BeginRequest(object sender, EventArgs e)
  96. //{
  97. // try
  98. // {
  99. // string session_param_name = "ASPSESSID";
  100. // string session_cookie_name = EMIS.Utility.Const.LOCAL_SETTING_LOGIN_COOKIENAME;
  101. // if (HttpContext.Current.Request.Form[session_param_name] != null)
  102. // {
  103. // UpdateCookie(session_cookie_name, HttpContext.Current.Request.Form[session_param_name]);
  104. // }
  105. // else if (HttpContext.Current.Request.QueryString[session_param_name] != null)
  106. // {
  107. // UpdateCookie(session_cookie_name, HttpContext.Current.Request.QueryString[session_param_name]);
  108. // }
  109. // }
  110. // catch
  111. // {
  112. // }
  113. // //此处是身份验证
  114. // try
  115. // {
  116. // string auth_param_name = "AUTHID";
  117. // string auth_cookie_name = FormsAuthentication.FormsCookieName;
  118. // if (HttpContext.Current.Request.Form[auth_param_name] != null)
  119. // {
  120. // UpdateCookie(auth_cookie_name, HttpContext.Current.Request.Form[auth_param_name]);
  121. // }
  122. // else if (HttpContext.Current.Request.QueryString[auth_param_name] != null)
  123. // {
  124. // UpdateCookie(auth_cookie_name, HttpContext.Current.Request.QueryString[auth_param_name]);
  125. // }
  126. // }
  127. // catch { }
  128. //}
  129. private void UpdateCookie(string cookie_name, string cookie_value)
  130. {
  131. HttpCookie cookie = HttpContext.Current.Request.Cookies.Get(cookie_name);
  132. if (null == cookie)
  133. {
  134. cookie = new HttpCookie(cookie_name);
  135. }
  136. cookie.Value = cookie_value;
  137. HttpContext.Current.Request.Cookies.Set(cookie);//重新设定请求中的cookie值,将服务器端的session值赋值给它
  138. }
  139. }
  140. }