的VirtualPathProvider不解析剃刀标记(VirtualPathProvider no

2019-10-17 06:23发布

我使用的是作为的VirtualPathProvider这个讨论中加载从数据库剃刀观点的文章。

问题是,认为负荷,剃刀标记不被解析的时候。 它被在页面上呈现的正是这样"@inherits System.Web.Mvc.WebViewPage hello world

对于这个例子,我已经硬编码到由返回的串GetContent()方法。

这里是我的代码:

public class MvcVirtualPathProvider : VirtualPathProvider {
    // virtualPath example >>  "~/Views/View/21EC2020-3AEA-1069-A2DD-08002B30309D.cshtml"
    private static bool IsPathVirtual(string virtualPath)
    {
        string path = VirtualPathUtility.ToAppRelative(virtualPath);

        // returns true if the path is requested by the "ViewController" controller
        if (path.StartsWith("~/Views/TextualReporting/db/", StringComparison.InvariantCultureIgnoreCase))
        {
            return true;
        }
        return false;
    }

    public override bool FileExists(string virtualPath) {
        if (IsPathVirtual(virtualPath)) {
            SimpleVirtualFile file = (SimpleVirtualFile)GetFile(virtualPath);
            if (file.Exists) {
                return true;
            }
            return false;
        }
        return Previous.FileExists(virtualPath);
    }

    public override VirtualFile GetFile(string virtualPath) {
        if (IsPathVirtual(virtualPath)) {
            return new SimpleVirtualFile(virtualPath);
        }
        return Previous.GetFile(virtualPath);
    }

    // Simply return the virtualPath on every request.
    public override string GetFileHash(string virtualPath, System.Collections.IEnumerable virtualPathDependencies) {
        if (IsPathVirtual(virtualPath)) {               
            return virtualPath;
        }
        return base.GetFileHash(virtualPath, virtualPathDependencies);
    }

    private class SimpleVirtualFile : VirtualFile {
        private string content;

        public bool Exists {
            get { return (content != null); }
        }

        public SimpleVirtualFile(string virtualPath)
            : base(virtualPath) {
            GetContent();
        }

        public override Stream Open() {
            ASCIIEncoding encoding = new ASCIIEncoding();
            return new MemoryStream(encoding.GetBytes(this.content), false);
        }

        protected void GetContent() {
            if (IsPathVirtual(VirtualPath)) {                   
                this.content = "@inherits System.Web.Mvc.WebViewPage \r\n hello world";                    
            }
        }
    }

    // just implement the default override and return null if the path is a Virtual Path
    public override CacheDependency GetCacheDependency(string virtualPath,
        System.Collections.IEnumerable virtualPathDependencies, DateTime utcStart) {
        if (IsPathVirtual(virtualPath)) {
            return null;
        }
        return Previous.GetCacheDependency(virtualPath, virtualPathDependencies, utcStart);
    }
}
文章来源: VirtualPathProvider not parsing razor markup