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 Bowin.Common; 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()); } //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) { //黑名单内的地址必须验证 if ((Const.LOCAL_AUTH_INCLUDEURL != null && Const.LOCAL_AUTH_INCLUDEURL.Any(w => Request.Path.ToLower().IndexOf(w.ToLower()) >= 0))) { FormsAuthenticationHelper fahelper = new FormsAuthenticationHelper(); var cookieName = EMIS.Utility.Const.LOCAL_SETTING_LOGIN_COOKIENAME; fahelper.AuthenticateRequest(HttpContext.Current, cookieName, Const.LOCAL_AUTH_EXCEPTURL, Const.LOCAL_AUTH_INCLUDEURL); var userInfo = EMIS.Utility.FormValidate.CustomPrincipal.Current; if (userInfo == null) { throw new HttpException(401, "该资源需要登录后访问"); } } } 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值赋值给它 } } }