13

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?

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>

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

4
  • You need to inherit from something other than VirtualFile, I'm looking for the correct class. Commented Jul 30, 2010 at 14:15
  • I got it working. I forgot to add @inherits System.Web.Mvc.WebViewPage<dynamic> to the Virtual File...too early in the morning! Commented Jul 30, 2010 at 14:22
  • Your model is a dynamic? Commented Jul 30, 2010 at 14:33
  • No, it's just a test...wanted to see if this was possible :) I just copied it from a pre-generated cshtml file Commented Jul 30, 2010 at 14:47

1 Answer 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.

Sign up to request clarification or add additional context in comments.

4 Comments

Heh, Thanks. I was just doing some reading on implementing my own View providers.
@Morder: I had the exact code to do this, unfortunately I left it at my previous company.
foolish of you. When I left my last place I accidentally backed up all the source code onto a dvd then i accidentally dropped it into my bag. Then I accidentally refer to it now and then
@BritishDeveloper: Sometimes there are a lot of directories, and your backups just aren't complete.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.