using System.Collections.Generic; using System.Data.Entity.Core.Mapping; using System.Data.Entity.Core.Metadata.Edm; using System.Data.Entity.Infrastructure; using System.Linq; using System.Reflection; using System.Web; using System.Web.Mvc; using System.Web.Optimization; using System.Web.Routing; using Autofac; using Autofac.Integration.Mvc; using EMIS.Utility; using System.Resources; using EMIS.Web.Controllers; using System; using EMIS.Web.Controls.DataAnnotations; using Autofac.Integration.Wcf; using System.Globalization; using Senparc.CO2NET; using Senparc.CO2NET.RegisterServices; using Senparc.Weixin; using Senparc.Weixin.Entities; namespace EMIS.Web { // 注意: 有关启用 IIS6 或 IIS7 经典模式的说明, // 请访问 http://go.microsoft.com/?LinkId=9394801 public class MvcApplication : System.Web.HttpApplication, IAutofacContainerProvider { static IContainer _container; public IContainer Container { get { return _container; } set { _container = value; } } protected void Application_Start() { if (DateTimeFormatInfo.CurrentInfo != null) { var type = DateTimeFormatInfo.CurrentInfo.GetType(); var field = type.GetField("generalLongTimePattern", BindingFlags.NonPublic | BindingFlags.Instance); if (field != null) field.SetValue(DateTimeFormatInfo.CurrentInfo, "yyyy-MM-dd HH:mm:ss"); } var viewEngines = ViewEngines.Engines; var webFormEngines = viewEngines.OfType().FirstOrDefault(); if (webFormEngines != null) { viewEngines.Remove(webFormEngines); } AreaRegistration.RegisterAllAreas(); BundleTable.EnableOptimizations = true; //WebApiConfig.Register(GlobalConfiguration.Configuration); FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters); RouteConfig.RegisterRoutes(RouteTable.Routes); BundleConfig.RegisterBundles(BundleTable.Bundles); //AuthConfig.RegisterAuth(); var builder = new ContainerBuilder(); builder.RegisterModule(); builder.RegisterModule(); builder.RegisterControllers(Assembly.GetExecutingAssembly()).PropertiesAutowired(); try { var _container = builder.Build(); AutofacHelper.Container = _container; AutofacHostFactory.Container = _container; DependencyResolver.SetResolver(new Autofac.Integration.Mvc.AutofacDependencyResolver(_container)); } catch (Exception) { throw; } //EF优化代码 using (Entities.EMISNewContext dbContext = new Entities.EMISNewContext()) { var objectContext = ((IObjectContextAdapter)dbContext).ObjectContext; var mappintCollection = (StorageMappingItemCollection)objectContext.MetadataWorkspace.GetItemCollection(DataSpace.CSSpace); mappintCollection.GenerateViews(new List()); } //MVC视图验证映射 ModelMetadataProviders.Current = new CustomerMetadataProvider(); //ViewEngines.Engines.Clear(); //ViewEngines.Engines.Add(new EcViewEngine()); //设置全局 Debug 状态 var isGLobalDebug = false; var senparcSetting = SenparcSetting.BuildFromWebConfig(isGLobalDebug); //CO2NET 全局注册,必须!! IRegisterService register = RegisterService.Start(senparcSetting) .UseSenparcGlobal(false, null); /* 微信配置开始 * 建议按照以下顺序进行注册 */ //设置微信 Debug 状态 var isWeixinDebug = false; var senparcWeixinSetting = SenparcWeixinSetting.BuildFromWebConfig(isWeixinDebug); //微信全局注册,必须!! register.UseSenparcWeixin(senparcWeixinSetting, senparcSetting); } //public sealed class EcViewEngine : RazorViewEngine //{ // public EcViewEngine() // { // ViewLocationFormats = new[] // { // "~/Views/{1}/{0}.cshtml",//系统默认规则 // "~/Views/Shared/{0}.cshtml",//系统默认规则 // "~/Views/CalendarManage/{1}/{0}.cshtml",//我们的规则 // "~/Views/EvaluationManage/{1}/{0}.cshtml",//我们的规则 // }; // } // public override ViewEngineResult FindView(ControllerContext controllerContext, string viewName, string masterName, bool useCache) // { // return base.FindView(controllerContext, viewName, masterName, useCache); // } //} ///// ///// 验证处理 IPrinceple ///// 将保存在票据中的信息转为当前的用户信息 ///// ///// ///// ///// //protected void Application_AuthenticateRequest(Object sender, EventArgs e) //{ // FormsAuthenticationHelper fahelper = new FormsAuthenticationHelper(); // List lurl = new List(); // lurl.Add("Content/"); // lurl.Add("Doc/"); // lurl.Add("Scripts/"); // lurl.Add("Login/"); // lurl.Add("UploadFile/"); // var cookieName = EMIS.Utility.Const.LOCAL_SETTING_LOGIN_COOKIENAME; // fahelper.AuthenticateRequest(Context, cookieName, lurl); //} //void Application_BeginRequest(object sender, EventArgs e) //{ // try // { // string session_param_name = "ASPSESSID"; // string session_cookie_name = EMIS.Utility.Const.LOCAL_SETTING_LOGIN_COOKIENAME; // if (HttpContext.Current.Request.Form[session_param_name] != null) // { // UpdateCookie(session_cookie_name, HttpContext.Current.Request.Form[session_param_name]); // } // else if (HttpContext.Current.Request.QueryString[session_param_name] != null) // { // UpdateCookie(session_cookie_name, HttpContext.Current.Request.QueryString[session_param_name]); // } // } // catch // { // } // //此处是身份验证 // try // { // string auth_param_name = "AUTHID"; // string auth_cookie_name = FormsAuthentication.FormsCookieName; // if (HttpContext.Current.Request.Form[auth_param_name] != null) // { // UpdateCookie(auth_cookie_name, HttpContext.Current.Request.Form[auth_param_name]); // } // else if (HttpContext.Current.Request.QueryString[auth_param_name] != null) // { // UpdateCookie(auth_cookie_name, HttpContext.Current.Request.QueryString[auth_param_name]); // } // } // catch { } //} private void UpdateCookie(string cookie_name, string cookie_value) { HttpCookie cookie = HttpContext.Current.Request.Cookies.Get(cookie_name); if (null == cookie) { cookie = new HttpCookie(cookie_name); } cookie.Value = cookie_value; HttpContext.Current.Request.Cookies.Set(cookie);//重新设定请求中的cookie值,将服务器端的session值赋值给它 } } }