Note that there are some explanatory texts on larger screens.

plurals
  1. POPulling a View from a database rather than a file
    text
    copied!<p>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.</p> <p>I think I asked this question too soon...I still look forward to any answers but I will definitely do some more research first.</p> <h2>Edit</h2> <p>So I wrote a quick sample that does what I want - to a point. I'll post updates as I get everything working properly.</p> <pre><code>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")); } } } </code></pre> <h2>Update</h2> <p>After playing around with it I found something interesting. If I remove the <code>return base...</code> from the <code>FileExists()</code> and <code>GetFile()</code> methods and only return true &amp; my <code>DbVirtualFile</code> the returned file is parsed and output is as expected. (Such as if I put <code>&lt;%:DateTime.Now.ToString()%&gt;</code>) - 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?</p> <h2>Final</h2> <p>It works. I just didn't add the inherits to the page I was testing. In this case: <code>@inherits System.Web.Mvc.WebViewPage&lt;dynamic&gt;</code></p> <p>Hope this helps someone else out there trying to do the same thing.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload