示例#1
0
        /// <summary>
        /// Reads the <see cref="RazorSourceDocument"/> from the specified <paramref name="projectItem"/>.
        /// </summary>
        /// <param name="projectItem">The <see cref="RazorProjectItem"/> to read from.</param>
        /// <returns>The <see cref="RazorSourceDocument"/>.</returns>
        public static RazorSourceDocument ReadFrom(RazorProjectItem projectItem)
        {
            if (projectItem == null)
            {
                throw new ArgumentNullException(nameof(projectItem));
            }

            // ProjectItem.PhysicalPath is usually an absolute (rooted) path.
            var filePath = projectItem.PhysicalPath;

            if (string.IsNullOrEmpty(filePath))
            {
                // Fall back to the relative path only if necessary.
                filePath = projectItem.RelativePhysicalPath;
            }

            if (string.IsNullOrEmpty(filePath))
            {
                // Then fall back to the FilePath (yeah it's a bad name) which is like an MVC view engine path
                // It's much better to have something than nothing.
                filePath = projectItem.FilePath;
            }

            if (projectItem.RazorSourceDocument is not null)
            {
                return(projectItem.RazorSourceDocument);
            }

            using (var stream = projectItem.Read())
            {
                // Autodetect the encoding.
                var relativePath = projectItem.RelativePhysicalPath ?? projectItem.FilePath;
                return(new StreamSourceDocument(stream, null, new RazorSourceDocumentProperties(filePath, relativePath)));
            }
        }
示例#2
0
        /// <summary>
        /// Reads the <see cref="RazorSourceDocument"/> from the specified <paramref name="projectItem"/>.
        /// </summary>
        /// <param name="projectItem">The <see cref="RazorProjectItem"/> to read from.</param>
        /// <returns>The <see cref="RazorSourceDocument"/>.</returns>
        public static RazorSourceDocument ReadFrom(RazorProjectItem projectItem)
        {
            if (projectItem == null)
            {
                throw new ArgumentNullException(nameof(projectItem));
            }

            var path = projectItem.PhysicalPath;

            if (string.IsNullOrEmpty(path))
            {
                path = projectItem.FilePath;
            }

            using (var inputStream = projectItem.Read())
            {
                return(ReadFrom(inputStream, path));
            }
        }