从数据库而不是文件中提取视图。

时间:2021-06-19 09:46:31

Is it possible to load a view from a database rather than from a file on disk? It doesn't necessarily have to be a database, could be any string really.

是否可能从数据库而不是从磁盘上的文件加载视图?它不一定是数据库,可以是任何字符串。

I think I asked this question too soon...I still look forward to any answers but I will definitely do some more research first.

我想我很快就问了这个问题……我仍然期待着任何答案,但我肯定会先做更多的研究。

Edit

So I wrote a quick sample that does what I want - to a point. I'll post updates as I get everything working properly.

所以我写了一个快速的样本它可以做我想做的事情,在一定程度上。当我让一切正常运行时,我会发布更新。

public class DbPathProvider : VirtualPathProvider {
    public DbPathProvider() : base() {

    }

    public override bool FileExists(string virtualPath) {
        if (virtualPath.StartsWith("/test") || virtualPath.StartsWith("~/test"))
            return true;

        return base.FileExists(virtualPath);
        //deal with this later
    }

    public override VirtualFile GetFile(string virtualPath) {
        if (virtualPath.StartsWith("/test") || virtualPath.StartsWith("~/test"))
            return new DbVirtualFile(virtualPath);

        return base.GetFile(virtualPath);
        //deal with this later
    }

    public class DbVirtualFile : System.Web.Hosting.VirtualFile {

        public DbVirtualFile(string path) : base (path) {
            //deal with this later
        }

        public override System.IO.Stream Open() {
            return new System.IO.MemoryStream(System.Text.ASCIIEncoding.ASCII.GetBytes("this is a test"));
        }
    }
}

Update

After playing around with it I found something interesting. If I remove the return base... from the FileExists() and GetFile() methods and only return true & my DbVirtualFile the returned file is parsed and output is as expected. (Such as if I put <%:DateTime.Now.ToString()%>) - however it doesn't when I add the tests and the base returns it just outputs a literal string of whatever is in my DbVirtualFile (god I hope this makes sense) - any thoughts out there?

玩了一圈之后,我发现了一些有趣的东西。如果我移除返回基数…通过FileExists()和GetFile()方法,只返回true & my DbVirtualFile,解析返回的文件并按预期输出。(例如,如果我输入<%:DateTime.Now.ToString()%>)——但是当我添加测试并返回基时,它并没有输出DbVirtualFile中任何内容的字符串(但愿这是有意义的)——有什么想法吗?

Final

It works. I just didn't add the inherits to the page I was testing. In this case: @inherits System.Web.Mvc.WebViewPage<dynamic>

它的工作原理。我只是没有将继承添加到我正在测试的页面。在这种情况下:@继承System.Web.Mvc.WebViewPage

Hope this helps someone else out there trying to do the same thing.

希望这能帮助其他人做同样的事情。

1 个解决方案

#1


4  

Yes, you'll have to create some new providers though. Here is a question that does basically the same thing, except from embedded files. This is an example that does exactly what you're looking for.

是的,您将不得不创建一些新的提供者。这里的问题基本上是一样的,除了嵌入的文件。这个例子就是你要找的。

#1


4  

Yes, you'll have to create some new providers though. Here is a question that does basically the same thing, except from embedded files. This is an example that does exactly what you're looking for.

是的,您将不得不创建一些新的提供者。这里的问题基本上是一样的,除了嵌入的文件。这个例子就是你要找的。