How to Load Views from Database in MVC

By June 16, 2014.NET, ASP.NET, C#, MVC, Web

We may need to store views in database tables and tell MVC engine to look for views from database instead on file system. This way, we can also define the hierarchy of pages. I’m going to use my previous implementation of loading views from assembly and with a few modifications, we will make that work for our database. First off, we will create a table named Page with the following schema.


We will now modify methods IsExistByVirtualPath and GetByVirtualPath which will look for views in table and return it’s content.


Views will now load from the database. Download the sample project from here.

Happy Coding!

And we know you want to learn more! So, register now for FalafelCon 2014 and perfect your skills, expand your horizons, and get inspired.

The following two tabs change content below.
  • David Moss

    Can someone please post the content of the database or at least the format of the VirtualPath (i.e. does it need to start with ~/) stored in the database. I don’t have SQL 2012 so I created the database in 2008 based on the schema from the article.

  • Amanda Fernandes

    Is someone having cache troubles?