/// <summary> /// Resolves the layout virtual path and ensures that file exist on the specified location. /// </summary> /// <param name="template">The template.</param> /// <returns> /// If the provided template is not in pure MVC mode returns null. /// If the provided template is in pure MVC mode returns virtual path like "~/SfLayouts/some_title.master" and ensures that this path is pointing to existing resource. /// Otherwise returns null. /// </returns> public virtual string GetVirtualPath(Pages.Model.IPageTemplate template) { if (template.GetTemplateFramework() != PageTemplateFramework.Mvc) { return(null); } var iHasTitle = template as IHasTitle; if (iHasTitle == null) { return(null); } string templateTitle = iHasTitle.GetTitle(); var vpBuilder = new LayoutVirtualPathBuilder(); var layoutVirtualPath = vpBuilder.BuildPathFromTitle(templateTitle); var doesLayoutExist = VirtualPathManager.FileExists(layoutVirtualPath); if (!doesLayoutExist) { layoutVirtualPath = null; } return(layoutVirtualPath); }
public void BuildPathFromTitle_TemplateTitle_ConstructsVirtualPath() { //Arrange var templateTitle = "TestTitle"; var expectedVirtualPath = "~/SfLayouts/TestTitle.master"; //Act var layoutVirtualPathBuilder = new LayoutVirtualPathBuilder(); var resultVirtualPath = layoutVirtualPathBuilder.BuildPathFromTitle(templateTitle); //Assert Assert.AreEqual(expectedVirtualPath, resultVirtualPath, "The virtual path is not constructed correctly."); }
public void GetLayoutName_WithoutMasterSuffix_ReturnsNull() { // Arrange var testVirtualPath = "~/TestPrefix/TestTitle.test"; var pathDefinition = new PathDefinition(); pathDefinition.VirtualPath = "~/TestPrefix"; // Act var layoutVirtualPathBuilder = new LayoutVirtualPathBuilder(); var resultLayoutName = layoutVirtualPathBuilder.GetLayoutName(pathDefinition, testVirtualPath); // Assert Assert.IsNull(resultLayoutName, "The result layout name should be null if the virtual path doesn't end with 'master'."); }
public void GetLayoutName_VirtualPath_ResolvesLayoutFileName() { // Arrange var expectedLayoutName = "TestTitle"; var testVirtualPath = "~/TestPrefix/TestTitle....master"; var pathDefinition = new PathDefinition { VirtualPath = "~/TestPrefix" }; // Act var layoutVirtualPathBuilder = new LayoutVirtualPathBuilder(); var resultLayoutName = layoutVirtualPathBuilder.GetLayoutName(pathDefinition, testVirtualPath); // Assert Assert.AreEqual(expectedLayoutName, resultLayoutName, "The layout name is not extracted correctly."); }
/// <summary> /// Creates a cache dependency based on the specified virtual paths. /// </summary> /// <param name="definition">The definition.</param> /// <param name="virtualPath">The path to the primary virtual resource.</param> /// <returns> /// A <see cref="T:System.Web.Caching.CacheDependency"/> object for the specified virtual resources. /// </returns> public CacheDependency GetCacheDependency(PathDefinition definition, string virtualPath, IEnumerable virtualPathDependencies, DateTime utcStart) { virtualPath = this.virtualPathBuilder.RemoveParams(virtualPath); virtualPath = VirtualPathUtility.ToAppRelative(virtualPath); var layoutVirtualPathBuilder = new LayoutVirtualPathBuilder(); string viewName = layoutVirtualPathBuilder.GetLayoutName(definition, virtualPath); var layoutTemplateBuilder = new LayoutRenderer(); if (string.IsNullOrEmpty(viewName)) return null; var viewPath = layoutTemplateBuilder.LayoutViewPath(viewName); return HostingEnvironment.VirtualPathProvider.GetCacheDependency(viewPath, virtualPathDependencies, utcStart); }
/// <summary> /// Determines whether a file with the specified virtual path exists. /// </summary> /// <param name="definition">The definition.</param> /// <param name="virtualPath">The virtual path to check.</param> public virtual bool Exists(PathDefinition definition, string virtualPath) { virtualPath = VirtualPathUtility.ToAppRelative(virtualPath); var vpBuilder = new LayoutVirtualPathBuilder(); string viewName = vpBuilder.GetLayoutName(definition, virtualPath); var layoutTemplateBuilder = new LayoutRenderer(); if (string.IsNullOrEmpty(viewName)) return false; if (virtualPath.StartsWith(string.Format("~/{0}", LayoutVirtualFileResolver.ResolverPath))) return layoutTemplateBuilder.LayoutExists(viewName); else return false; }
/// <summary> /// Determines whether a file with the specified virtual path exists. /// </summary> /// <param name="definition">The definition.</param> /// <param name="virtualPath">The virtual path to check.</param> public virtual bool Exists(PathDefinition definition, string virtualPath) { virtualPath = this.virtualPathBuilder.RemoveParams(virtualPath); virtualPath = VirtualPathUtility.ToAppRelative(virtualPath); var layoutVirtualPathBuilder = new LayoutVirtualPathBuilder(); string viewName = layoutVirtualPathBuilder.GetLayoutName(definition, virtualPath); var layoutTemplateBuilder = new LayoutRenderer(); if (string.IsNullOrEmpty(viewName)) return false; if (virtualPath.StartsWith(string.Format(System.Globalization.CultureInfo.InvariantCulture, "~/{0}", LayoutVirtualFileResolver.ResolverPath), StringComparison.Ordinal)) return layoutTemplateBuilder.LayoutExists(viewName); else return false; }
/// <summary> /// Opens the the file with the specified virtual path. /// </summary> /// <param name="virtualPath">The virtual path of the file to open.</param> public virtual Stream Open(PathDefinition definition, string virtualPath) { MemoryStream outPutStream = null; virtualPath = VirtualPathUtility.ToAppRelative(virtualPath); var vpBuilder = new LayoutVirtualPathBuilder(); string viewName = vpBuilder.GetLayoutName(definition, virtualPath); var layoutHtmlString = this.RenderLayout(viewName); if (!string.IsNullOrEmpty(layoutHtmlString)) { var layoutBytes = Encoding.Default.GetBytes(layoutHtmlString.ToCharArray()); outPutStream = new MemoryStream(layoutBytes); } return(outPutStream); }
/// <summary> /// Creates a cache dependency based on the specified virtual paths. /// </summary> /// <param name="definition">The definition.</param> /// <param name="virtualPath">The path to the primary virtual resource.</param> /// <returns> /// A <see cref="T:System.Web.Caching.CacheDependency"/> object for the specified virtual resources. /// </returns> public CacheDependency GetCacheDependency(PathDefinition definition, string virtualPath, IEnumerable virtualPathDependencies, DateTime utcStart) { virtualPath = this.virtualPathBuilder.RemoveParams(virtualPath); virtualPath = VirtualPathUtility.ToAppRelative(virtualPath); var layoutVirtualPathBuilder = new LayoutVirtualPathBuilder(); string viewName = layoutVirtualPathBuilder.GetLayoutName(definition, virtualPath); var layoutTemplateBuilder = new LayoutRenderer(); if (string.IsNullOrEmpty(viewName)) { return(null); } var viewPath = layoutTemplateBuilder.LayoutViewPath(viewName); return(HostingEnvironment.VirtualPathProvider.GetCacheDependency(viewPath, virtualPathDependencies, utcStart)); }
/// <summary> /// Opens the the file with the specified virtual path. /// </summary> /// <param name="virtualPath">The virtual path of the file to open.</param> public virtual Stream Open(PathDefinition definition, string virtualPath) { virtualPath = this.virtualPathBuilder.RemoveParams(virtualPath); MemoryStream outPutStream = null; virtualPath = VirtualPathUtility.ToAppRelative(virtualPath); var virtualBuilder = new LayoutVirtualPathBuilder(); var viewName = virtualBuilder.GetLayoutName(definition, virtualPath); var layoutHtmlString = this.RenderLayout(viewName); if (!string.IsNullOrEmpty(layoutHtmlString)) { var layoutBytes = Encoding.Default.GetBytes(layoutHtmlString.ToCharArray()); outPutStream = new MemoryStream(layoutBytes); } return outPutStream; }
/// <summary> /// Resolves the layout virtual path and ensures that file exist on the specified location. /// </summary> /// <param name="template">The template.</param> /// <returns> /// If the provided template is not in pure MVC mode returns null. /// If the provided template is in pure MVC mode returns virtual path like "~/SfLayouts/some_title.master" and ensures that this path is pointing to existing resource. /// Otherwise returns null. /// </returns> public virtual string GetVirtualPath(Pages.Model.IPageTemplate template) { if (template.GetTemplateFramework() != PageTemplateFramework.Mvc) return null; var iHasTitle = template as IHasTitle; if (iHasTitle == null) return null; string templateTitle = iHasTitle.GetTitle(); var vpBuilder = new LayoutVirtualPathBuilder(); var layoutVirtualPath = vpBuilder.BuildPathFromTitle(templateTitle); var doesLayoutExist = VirtualPathManager.FileExists(layoutVirtualPath); if (!doesLayoutExist) layoutVirtualPath = null; return layoutVirtualPath; }
/// <summary> /// Opens the the file with the specified virtual path. /// </summary> /// <param name="virtualPath">The virtual path of the file to open.</param> public virtual Stream Open(PathDefinition definition, string virtualPath) { var placeholdersOnly = virtualPath.EndsWith(".master", StringComparison.OrdinalIgnoreCase); virtualPath = this.virtualPathBuilder.RemoveParams(virtualPath); MemoryStream outPutStream = null; virtualPath = VirtualPathUtility.ToAppRelative(virtualPath); var virtualBuilder = new LayoutVirtualPathBuilder(); var viewName = virtualBuilder.GetLayoutName(definition, virtualPath); var layoutHtmlString = this.RenderLayout(viewName, placeholdersOnly); if (!string.IsNullOrEmpty(layoutHtmlString)) { var layoutBytes = Encoding.UTF8.GetBytes(layoutHtmlString.ToCharArray()); outPutStream = new MemoryStream(layoutBytes); } return(outPutStream); }
/// <summary> /// Determines whether a file with the specified virtual path exists. /// </summary> /// <param name="definition">The definition.</param> /// <param name="virtualPath">The virtual path to check.</param> public virtual bool Exists(PathDefinition definition, string virtualPath) { virtualPath = VirtualPathUtility.ToAppRelative(virtualPath); var vpBuilder = new LayoutVirtualPathBuilder(); string viewName = vpBuilder.GetLayoutName(definition, virtualPath); var layoutTemplateBuilder = new LayoutRenderer(); if (string.IsNullOrEmpty(viewName)) { return(false); } if (virtualPath.StartsWith(string.Format("~/{0}", LayoutVirtualFileResolver.ResolverPath))) { return(layoutTemplateBuilder.LayoutExists(viewName)); } else { return(false); } }
/// <summary> /// Determines whether a file with the specified virtual path exists. /// </summary> /// <param name="definition">The definition.</param> /// <param name="virtualPath">The virtual path to check.</param> public virtual bool Exists(PathDefinition definition, string virtualPath) { virtualPath = this.virtualPathBuilder.RemoveParams(virtualPath); virtualPath = VirtualPathUtility.ToAppRelative(virtualPath); var layoutVirtualPathBuilder = new LayoutVirtualPathBuilder(); string viewName = layoutVirtualPathBuilder.GetLayoutName(definition, virtualPath); var layoutTemplateBuilder = new LayoutRenderer(); if (string.IsNullOrEmpty(viewName)) { return(false); } if (virtualPath.StartsWith(string.Format(System.Globalization.CultureInfo.InvariantCulture, "~/{0}", LayoutVirtualFileResolver.ResolverPath), StringComparison.Ordinal)) { return(layoutTemplateBuilder.LayoutExists(viewName)); } else { return(false); } }
/// <summary> /// Opens the the file with the specified virtual path. /// </summary> /// <param name="virtualPath">The virtual path of the file to open.</param> public virtual Stream Open(PathDefinition definition, string virtualPath) { var placeholdersOnly = virtualPath.EndsWith(".master", StringComparison.OrdinalIgnoreCase); virtualPath = this.virtualPathBuilder.RemoveParams(virtualPath); MemoryStream outPutStream = null; virtualPath = VirtualPathUtility.ToAppRelative(virtualPath); var virtualBuilder = new LayoutVirtualPathBuilder(); var viewName = virtualBuilder.GetLayoutName(definition, virtualPath); var layoutHtmlString = this.RenderLayout(viewName, placeholdersOnly); if (!string.IsNullOrEmpty(layoutHtmlString)) { var layoutBytes = Encoding.UTF8.GetBytes(layoutHtmlString.ToCharArray()); outPutStream = new MemoryStream(layoutBytes); } return outPutStream; }