示例#1
0
        static AreaRegistrationBase()
        {
            XTrace.WriteLine("{0} Start 初始化魔方 {0}", new String('=', 32));
            Assembly.GetExecutingAssembly().WriteVersion();

            // 遍历所有引用了AreaRegistrationBase的程序集
            var list = new List <PrecompiledViewAssembly>();

            foreach (var asm in FindAllArea())
            {
                XTrace.WriteLine("注册区域视图程序集:{0}", asm.FullName);

                var pva = new PrecompiledViewAssembly(asm);
                list.Add(pva);
            }
            PrecompiledEngines = list.ToArray();

            var engine = new CompositePrecompiledMvcEngine(PrecompiledEngines);

            XTrace.WriteLine("注册复合预编译引擎,共有视图程序集{0}个", list.Count);
            //ViewEngines.Engines.Insert(0, engine);
            // 预编译引擎滞后,让其它引擎先工作
            ViewEngines.Engines.Add(engine);

            // StartPage lookups are done by WebPages.
            VirtualPathFactoryManager.RegisterVirtualPathFactory(engine);

            // 注册绑定提供者
            EntityModelBinderProvider.Register();

            // 注册过滤器
            XTrace.WriteLine("注册过滤器:{0}", typeof(MvcHandleErrorAttribute).FullName);
            XTrace.WriteLine("注册过滤器:{0}", typeof(EntityAuthorizeAttribute).FullName);
            var filters = GlobalFilters.Filters;

            filters.Add(new MvcHandleErrorAttribute());
            filters.Add(new EntityAuthorizeAttribute()
            {
                IsGlobal = true
            });

            // 从数据库或者资源文件加载模版页面的例子
            //HostingEnvironment.RegisterVirtualPathProvider(new ViewPathProvider());

            //var routes = RouteTable.Routes;
            //routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
            //routes.MapMvcAttributeRoutes();

            //routes.MapRoute(
            //    name: "Virtual",
            //    url: "{*viewName}",
            //    defaults: new { controller = "Frontend", action = "Default" },
            //    constraints: new { controller = "Frontend", action = "Default" }
            //);

            // 自动检查并下载魔方资源
            Task.Factory.StartNew(CheckContent, TaskCreationOptions.LongRunning).LogException();

            XTrace.WriteLine("{0} End   初始化魔方 {0}", new String('=', 32));
        }
示例#2
0
        static AreaRegistrationBase()
        {
            XTrace.WriteLine("{0} Start 初始化魔方 {0}", new String('=', 32));
            Assembly.GetExecutingAssembly().WriteVersion();

            var ioc      = ObjectContainer.Current;
            var services = ioc.BuildServiceProvider();

            //#if !NET4
            //            // 外部管理提供者需要手工覆盖
            //            ioc.AddSingleton<IManageProvider, DefaultManageProvider>();
            //#endif
            if (ManageProvider.Provider == null)
            {
                ManageProvider.Provider = new DefaultManageProvider();
            }

            // 遍历所有引用了AreaRegistrationBase的程序集
            var list = new List <PrecompiledViewAssembly>();

            foreach (var asm in FindAllArea())
            {
                XTrace.WriteLine("注册区域视图程序集:{0}", asm.FullName);

                list.Add(new PrecompiledViewAssembly(asm));
            }
            PrecompiledEngines = list.ToArray();

            var engine = new CompositePrecompiledMvcEngine(PrecompiledEngines);

            XTrace.WriteLine("注册复合预编译引擎,共有视图程序集{0}个", list.Count);
            //ViewEngines.Engines.Insert(0, engine);
            // 预编译引擎滞后,让其它引擎先工作
            ViewEngines.Engines.Add(engine);

            // StartPage lookups are done by WebPages.
            VirtualPathFactoryManager.RegisterVirtualPathFactory(engine);

            // 注册绑定提供者
            //ioc.Register<IModelBinderProvider, EntityModelBinderProvider>("Entity");
            //ioc.Register<IModelBinderProvider, PagerModelBinderProvider>("Pager");
            var providers = ModelBinderProviders.BinderProviders;
            //var prv = ioc.Resolve<IModelBinderProvider>("Entity");
            var prv = services.GetService(typeof(EntityModelBinderProvider)) as IModelBinderProvider;

            if (prv == null)
            {
                prv = new EntityModelBinderProvider();
            }
            if (prv != null)
            {
                XTrace.WriteLine("注册实体模型绑定器:{0}", prv.GetType().FullName);
                providers.Add(prv);
            }
            //prv = ioc.Resolve<IModelBinderProvider>("Pager");
            var prv2 = services.GetService(typeof(PagerModelBinderProvider)) as IModelBinderProvider;

            if (prv2 == null)
            {
                prv2 = new PagerModelBinderProvider();
            }
            if (prv2 != null)
            {
                XTrace.WriteLine("注册页面模型绑定器:{0}", prv2.GetType().FullName);
                providers.Add(prv2);
            }

            // 注册过滤器
            //ioc.Register<HandleErrorAttribute, MvcHandleErrorAttribute>();
            //ioc.Register<AuthorizeAttribute, EntityAuthorizeAttribute>("Cube");
            var filters = GlobalFilters.Filters;
            //var f1 = ioc.Resolve<HandleErrorAttribute>();
            var f1 = services.GetService(typeof(HandleErrorAttribute)) as HandleErrorAttribute;

            if (f1 != null)
            {
                XTrace.WriteLine("注册异常过滤器:{0}", f1.GetType().FullName);
                filters.Add(f1);
            }
            //var f2 = ioc.Resolve<AuthorizeAttribute>();
            var f2 = services.GetService(typeof(AuthorizeAttribute)) as AuthorizeAttribute;

            if (f2 == null)
            {
                f2 = new EntityAuthorizeAttribute();
            }
            if (f2 != null)
            {
                XTrace.WriteLine("注册授权过滤器:{0}", f2.GetType().FullName);
                if (f2 is EntityAuthorizeAttribute eaa)
                {
                    eaa.IsGlobal = true;
                }
                filters.Add(f2);
            }
            //foreach (var item in ioc.ResolveAll(typeof(AuthorizeAttribute)))
            //{
            //    var auth = item.Instance;
            //    XTrace.WriteLine("注册[{0}]授权过滤器:{1}", item.Identity, auth.GetType().FullName);
            //    if (auth is EntityAuthorizeAttribute eaa) eaa.IsGlobal = true;
            //    filters.Add(auth);
            //}

            // 从数据库或者资源文件加载模版页面的例子
            //HostingEnvironment.RegisterVirtualPathProvider(new ViewPathProvider());

            //var routes = RouteTable.Routes;
            //routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
            //routes.MapMvcAttributeRoutes();

            //routes.MapRoute(
            //    name: "Virtual",
            //    url: "{*viewName}",
            //    defaults: new { controller = "Frontend", action = "Default" },
            //    constraints: new { controller = "Frontend", action = "Default" }
            //);

            var routes = RouteTable.Routes;

            if (routes["Cube"] == null)
            {
                // 为魔方注册默认首页,启动魔方站点时能自动跳入后台,同时为Home预留默认过度视图页面
                routes.MapRoute(
                    name: "CubeHome",
                    url: "CubeHome/{action}/{id}",
                    defaults: new { controller = "CubeHome", action = "Index", id = UrlParameter.Optional },
                    namespaces: new[] { typeof(CubeHomeController).Namespace }
                    );
                routes.MapRoute(
                    name: "Cube",
                    url: "Cube/{action}/{id}",
                    defaults: new { controller = "Cube", action = "Info", id = UrlParameter.Optional },
                    namespaces: new[] { typeof(CubeController).Namespace }
                    );
            }
            if (routes["Sso"] == null)
            {
                routes.MapRoute(
                    name: "Sso",
                    url: "Sso/{action}/{id}",
                    defaults: new { controller = "Sso", action = "Index", id = UrlParameter.Optional },
                    namespaces: new[] { typeof(CubeHomeController).Namespace }
                    );
            }

            // 自动检查并下载魔方资源
            ThreadPoolX.QueueUserWorkItem(CheckContent);

            XTrace.WriteLine("{0} End   初始化魔方 {0}", new String('=', 32));
        }