从数据库中提取一个视图,而不是一个文件 [英] Pulling a View from a database rather than a file

查看:157
本文介绍了从数据库中提取一个视图,而不是一个文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有可能从一个数据库,而不是从磁盘上的文件中加载的看法?它不一定必须是一个数据库,可以是任意字符串确实

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.

所以我写了一个快速的样品,做我想要的东西 - 一个点。我得到的一切工作正常,我会发布更新。

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

更新

玩弄它后,我发现一些有趣的事情。如果我删除返回基地... FILEEXISTS()的GetFile() 方法,只返回true&安培;我的 DbVirtualFile 返回的文件进行解析,并如预期的输出。 (比如,如果我把<%:DateTime.Now.ToString()%GT; ) - 然而,它没有当我添加了测试和底座返回它只是输出的无论是在我的DbVirtualFile一个字符串(上帝,我希望这是有道理的) - 任何想法在那里

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?

它的工作原理。我只是没有继承的添加到我测试的页面。在这种情况下: @inherits System.Web.Mvc.WebViewPage&LT;动态&GT;

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

希望这有助于别人那里试图做同样的事情。

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

推荐答案

是的,你必须创造,虽然一些新的供应商。 <一href=\"http://stackoverflow.com/questions/236972/using-virtualpathprovider-to-load-asp-net-mvc-views-from-dlls\">Here是做基本上是同样的事情,除了从嵌入的文件的问题。 是,它正是你要寻找的一个例子

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.

这篇关于从数据库中提取一个视图,而不是一个文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆