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 EMISOnline.Utility; using System.Resources; namespace EMISOnline.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() { 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(); var _container = builder.Build(); AutofacHelper.Container = _container; DependencyResolver.SetResolver(new Autofac.Integration.Mvc.AutofacDependencyResolver(_container)); //EF优化代码 using (Entities.EMISOnlineContextContainer dbContext = new Entities.EMISOnlineContextContainer()) { var objectContext = ((IObjectContextAdapter)dbContext).ObjectContext; var mappintCollection = (StorageMappingItemCollection)objectContext.MetadataWorkspace.GetItemCollection(DataSpace.CSSpace); mappintCollection.GenerateViews(new List()); } //ViewEngines.Engines.Clear(); //ViewEngines.Engines.Add(new EcViewEngine()); } //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值赋值给它 } } }