VirtualPathProvider not parsing razor markup

元气小坏坏 提交于 2019-12-13 02:14:09

问题


I am using a VirtualPathProvider to load razor views from the database as discussed in this article.

The problem is when the view loads, the Razor markup is not being parsed. It is being rendered on the page exactly like this "@inherits System.Web.Mvc.WebViewPage hello world"

For this example, i have hard-coded the string to be returned by the GetContent() method.

Here is my code:

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);
    }
}

来源:https://stackoverflow.com/questions/12628620/virtualpathprovider-not-parsing-razor-markup

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!