/// <summary> /// 尝试获取缓存的输出 /// </summary> /// <returns>缓存的输出</returns> protected virtual ICachedResponse ResolveCache(string virtualPath) { var policy = HtmlServices.GetCachePolicy(HttpContext); if (policy == null) { return(null); } CachePolicy = policy; var clientCachePolicy = policy as IClientCacheablePolicy; if (clientCachePolicy != null) { var response = clientCachePolicy.ResolveClientCache(); if (response != null) { return(response); } } return(CachePolicy.ResolveCache()); }
/// <summary> /// 加载 HTML 文档 /// </summary> /// <param name="virtualPath">文档的虚拟路径</param> /// <param name="cacheDependency">若文档已被缓存,获取缓存依赖项</param> /// <returns>HTML 文档对象</returns> public static IHtmlDocument LoadDocument(string virtualPath, out CacheDependency cacheDependency) { string cacheKey; var result = LoadDocument(virtualPath, out cacheKey); if (result == null) { cacheDependency = null; return(result); } if (cacheKey != null) { cacheDependency = new CacheDependency(new string[0], new string[] { cacheKey }); } else { cacheDependency = HtmlServices.CreateCacheDependency(virtualPath); } return(result); }
/// <summary> /// 渲染部分视图 /// </summary> /// <param name="context">当前 HTTP 请求上下文</param> /// <param name="virtualPath">部分视图的虚拟路径</param> /// <returns>渲染结果</returns> public static string RenderPartial(HttpContextBase context, string virtualPath) { var handler = HtmlHandlerProvider.GetHandler(virtualPath); if (handler == null) { return(HtmlServices.LoadContent(virtualPath).Content); } var partialHandler = new JumonyPartialHandler(); return(partialHandler.RenderPartial(context, virtualPath, handler)); }
private static Uri CreateDocumentUri(string virtualPath) { if (virtualPath == null) { throw new ArgumentNullException("virtualPath"); } if (!VirtualPathUtility.IsAppRelative(virtualPath)) { throw HtmlServices.VirtualPathFormatError("virtualPath"); } return(new Uri(baseUri, VirtualPathUtility.ToAbsolute(virtualPath))); }
/// <summary> /// 派生类可以重写此方法自定义加载虚拟路径处的文档的逻辑 /// </summary> /// <returns></returns> protected virtual IHtmlDocument LoadDocument(string virtualPath, out CacheDependency cacheDependency) { string cacheKey; var document = HtmlServices.LoadDocument(virtualPath, out cacheKey); if (cacheKey != null) { cacheDependency = new CacheDependency(new string[0], new string[] { cacheKey }); } else { cacheDependency = HtmlServices.CreateCacheDependency(virtualPath); } return(document); }
/// <summary> /// 在 HTML 文档中查找 ViewHandler 路径设置。 /// </summary> /// <param name="virtualPath">要处理的 HTML 文档的虚拟路径</param> /// <returns>用于处理 HTML 的视图处理程序路径</returns> public static string GetHandlerPath(string virtualPath) { var cacheKey = handlerPathCachePrefix + virtualPath; var cacheItem = HttpRuntime.Cache.Get(cacheKey) as HandlerPathCacheItem; if (cacheItem == null) { string cacheDependencyKey; var document = HtmlServices.LoadDocument(virtualPath, out cacheDependencyKey); if (document == null) { return(null); } var head = document.FindFirstOrDefault("head"); if (head == null) { return(null); } var handlerMeta = head.FindFirstOrDefault("meta[name=handler]"); if (handlerMeta == null) { return(null); } var handlerPath = handlerMeta.Attribute("value").Value(); if (cacheDependencyKey != null) { cacheItem = new HandlerPathCacheItem() { HandlerPath = handlerPath }; HttpRuntime.Cache.Insert(cacheKey, cacheItem, new CacheDependency(new string[0], new[] { cacheDependencyKey })); } } return(cacheItem.HandlerPath); }
/// <summary> /// 派生类重写此方法自定义加载文档的逻辑 /// </summary> /// <param name="virtualPath">文档的虚拟路径</param> /// <returns>加载的文档对象</returns> protected virtual IHtmlDocument LoadDocument(string virtualPath) { IHtmlDocument document; Trace("Begin load document."); document = HtmlServices.LoadDocument(virtualPath); if (document == null) { throw new HttpException(404, "加载文档失败"); } Trace("End load document."); return(document); }
/// <summary> /// 利用指定 VirtualPathProvider 将虚拟路径所指向文件当作静态文件加载。 /// </summary> /// <param name="provider">指定的 VirtualPathProvider</param> /// <param name="virtualPath">虚拟路径</param> /// <returns>加载结果</returns> public HtmlContentResult LoadContent(VirtualPathProvider provider, string virtualPath) { if (!VirtualPathUtility.IsAppRelative(virtualPath)) { return(null); } if (!provider.FileExists(virtualPath)) { return(null); } var file = provider.GetFile(virtualPath); if (file == null) { return(null); } var key = provider.GetCacheKey(virtualPath) ?? "StaticFile_" + virtualPath; var content = HttpRuntime.Cache.Get(key) as string; if (content == null) { var dependency = HtmlServices.CreateCacheDependency(provider, virtualPath); content = LoadContent(file); HttpRuntime.Cache.Insert(key, content, dependency); } return(new HtmlContentResult(content, key)); }
public CachePolicy CreateCachePolicy(HttpContextBase context) { return(HtmlServices.GetCachePolicy(context)); }