Example #1
0
        public void RegisterTest()
        {
            var service1 = "~/test/1.html";
            var service2 = "~/test/";
            var service3 = "~/";

            var globalService = "global";

            WebServiceLocator.RegisterService(service1, "~/test/1.html");
            WebServiceLocator.RegisterService(service2, "~/test/");
            WebServiceLocator.RegisterService(service3, "~/");
            WebServiceLocator.RegisterService(globalService);

            string path     = "~/test/1.html";
            var    services = WebServiceLocator.GetServices <string>(path);


            Assert.Contains(services, service1, "注册服务在页面失败");
            Assert.Contains(services, service2, "注册服务在目录失败");
            Assert.Contains(services, service3, "注册服务在根目录失败");
            Assert.Contains(services, globalService, "注册全局服务失败");

            WebServiceLocator.UnregisterService(service1, path);
            Assert.NotContains(WebServiceLocator.GetServices <string>(path), service1, "在页面解除注册服务失败");

            WebServiceLocator.UnregisterService(service2, path);
            Assert.NotContains(WebServiceLocator.GetServices <string>(path), service2, "在目录解除注册服务失败");

            WebServiceLocator.UnregisterService(service3, path);
            Assert.NotContains(WebServiceLocator.GetServices <string>(path), service3, "在根目录解除注册服务失败");

            WebServiceLocator.UnregisterService(globalService);
            Assert.NotContains(WebServiceLocator.GetServices <string>(path), globalService, "解除注册全局服务失败");
        }
Example #2
0
 /// <summary>
 /// 获取 HTML 筛选器
 /// </summary>
 /// <param name="virtualPath">HTML 文档虚拟路径</param>
 /// <returns>HTML 筛选器</returns>
 protected virtual IHtmlFilter[] GetFilters(string virtualPath)
 {
     return(WebServiceLocator
            .GetServices <IHtmlFilterProvider>(virtualPath)
            .SelectMany(p => p.GetFilters())
            .Reverse().ToArray());
 }
Example #3
0
        /// <summary>
        /// 获取指定虚拟路径的 HTML 处理程序
        /// </summary>
        /// <param name="virtualPath">要获取 HTML 处理程序的虚拟路径</param>
        /// <returns>HTML 处理程序</returns>
        public static IHtmlHandler GetHandler(string virtualPath)
        {
            var services = WebServiceLocator.GetServices <IHtmlHandlerProvider>(virtualPath);

            foreach (var provider in services)
            {
                var handler = provider.GetHandler(virtualPath);

                if (handler != null)
                {
                    return(handler);
                }
            }


            if (!HostingEnvironment.VirtualPathProvider.FileExists(virtualPath))//如果文件不存在,则直接返回 null 。
            {
                return(null);
            }


            return
                (GetHandlerInternal <IHtmlHandler>(virtualPath + ".ashx") ??
                 GetHandlerInternal <IHtmlHandler>(GetHandlerPath(virtualPath)) ??
                 GetHandlerInternal <IHtmlHandler>(VirtualPathHelper.FallbackSearch(virtualPath, "_handler.ashx")));
        }
Example #4
0
        /// <summary>
        /// 加载 HTML 文档内容
        /// </summary>
        /// <param name="virtualPath">文档的虚拟路径</param>
        /// <returns>HTML 内容加载结果</returns>
        public static HtmlContentResult LoadContent(string virtualPath)
        {
            if (virtualPath == null)
            {
                throw new ArgumentNullException("virtualPath");
            }


            if (!VirtualPathUtility.IsAppRelative(virtualPath))
            {
                throw VirtualPathFormatError("virtualPath");
            }



            var services = WebServiceLocator.GetServices <IHtmlContentProvider>(virtualPath).Concat(Default.GetContentServices(virtualPath));

            foreach (var provider in services)
            {
                var result = provider.LoadContent(virtualPath);

                if (result != null)
                {
                    result.Provider    = provider;
                    result.VirtualPath = virtualPath;
                    return(result);
                }
            }


            return(null);
        }
Example #5
0
        /// <summary>
        /// 获取当前视图所需要应用的筛选器。
        /// </summary>
        /// <returns></returns>
        protected virtual IViewFilter[] GetFilters(ViewContext context)
        {
            var filterProviders = WebServiceLocator.GetServices <IViewFilterProvider>(VirtualPath);
            var filters         = filterProviders.SelectMany(p => p.GetFilters(VirtualPath)).Reverse();

            filters = filters.Concat(context.ViewData[ViewFiltersDataKey] as IEnumerable <IViewFilter> ?? Enumerable.Empty <IViewFilter>());

            return(filters.ToArray());
        }
Example #6
0
        /// <summary>
        /// 查找母板视图的处理程序
        /// </summary>
        /// <param name="virtualPath">母板视图虚拟路径</param>
        /// <returns>视图处理程序</returns>
        public static IViewHandler GetMasterViewHandler(string virtualPath)
        {
            foreach (var provider in WebServiceLocator.GetServices <IViewHandlerProvider>(virtualPath))
            {
                var handler = provider.FindViewHandler(virtualPath);
                if (handler != null)
                {
                    return(handler);
                }
            }

            return(GetViewHandlerInternal(virtualPath + ".ashx", false));
        }
Example #7
0
        /// <summary>
        /// 获取当前请求的缓存策略
        /// </summary>
        /// <param name="context">当前 HTTP 请求</param>
        /// <returns>适用于当前请求的缓存策略</returns>
        public static CachePolicy GetCachePolicy(HttpContextBase context)
        {
            var virtualPath = context.Request.AppRelativeCurrentExecutionFilePath;

            foreach (var provider in WebServiceLocator.GetServices <ICachePolicyProvider>(virtualPath))
            {
                CachePolicy policy = provider.CreateCachePolicy(context);
                if (policy != null)
                {
                    return(policy);
                }
            }

            return(null);
        }
Example #8
0
        /// <summary>
        /// 获取用于分析 HTML 文档的分析器
        /// </summary>
        /// <param name="contentResult">文档内容加载结果</param>
        /// <returns>HTML 分析器相关信息</returns>
        public static IHtmlParser GetParser(HtmlContentResult contentResult)
        {
            if (contentResult == null)
            {
                throw new ArgumentNullException("contentResult");
            }



            foreach (var provider in WebServiceLocator.GetServices <IHtmlParserProvider>(contentResult.VirtualPath))
            {
                var parser = provider.GetParser(contentResult.VirtualPath, contentResult.Content);
                if (parser != null)
                {
                    return(parser);
                }
            }

            return(Default.GetParser(contentResult.VirtualPath));
        }
Example #9
0
        /// <summary>
        /// 获取当前请求的缓存策略
        /// </summary>
        /// <param name="context">当前请求的 MVC 上下文</param>
        /// <param name="action">请求的 Action</param>
        /// <param name="parameters">Action 的参数信息</param>
        /// <returns></returns>
        public static CachePolicy CreateCachePolicy(ControllerContext context, ActionDescriptor action, IDictionary <string, object> parameters)
        {
            foreach (var provider in WebServiceLocator.GetServices <IMvcCachePolicyProvider>())
            {
                var policy = provider.CreateCachePolicy(context, action, parameters);
                if (policy != null)
                {
                    return(policy);
                }
            }


            if (Configuration.FallbackCachePolicy)
            {
                return(FallbackCachePolicyProvider.CreateCachePolicy(context, action, parameters));
            }

            else
            {
                return(null);
            }
        }