Sunday, June 15, 2014

Implementing an ASP.NET VirtualPathProvider

If you are looking to implement a VirtualPathProvider for rendering virtual .aspx pages (such as from a database) rather than directly from the File System (similar to how SharePoint renders pages), then you will definitely want to follow this article:  http://support.microsoft.com/kb/910441

Unfortunately, the article leaves out some critical information:


  1. You will need to pre-populate the data into the database with some sample .aspx pages otherwise the AdministrationPage will not render
  2. The code to retrieve the information from the database and use it as part of the VirtualPathProvider is left out from the DBUtility class!!


However, after reading through some of the comments, I was able to fill in the missing information in order to be able to get a functional application rendering .aspx pages directly out of the database.

Below is the complete DBUtility code for your viewing pleasure:

using System;

using System.Collections;

using System.Collections.Generic;

using System.Configuration;

using System.Data.SqlClient;

using System.IO;

using System.Linq;

using System.Text;

 

namespace SPVPP

{

    public class DBUtility

    {

        SqlConnection cnn;

        private string connectionString = ConfigurationManager.ConnectionStrings["VirtualProviderDBConnectionString"].ConnectionString;

 

        //Run a select query to obtain all files and their content, and 

        //store the result in a in memory hashtable to obtain fast access.    

        string cmdSelectAllFiles = "SELECT FileName,FileData FROM VirtualFileSystem";

        Hashtable virtualFiles = null;

 

        public DBUtility() { virtualFiles = new Hashtable(); }

 

        public Hashtable GetVirtualFiles()

        {

            /* 1. Open a connection.  

               2. Select all the files.

               3. Iterate through the result and store the file name as a Key and

                  content as a Value in a hashtable.

               4. Finally return hashtable.

            */

            virtualFiles = new Hashtable();

            cnn = new SqlConnection(connectionString);

            cnn.Open();

 

            SqlCommand cmd = cnn.CreateCommand();

            cmd.CommandText = cmdSelectAllFiles;

 

            using (SqlDataReader sqlDr = cmd.ExecuteReader())

            {

                while (sqlDr.Read())

                {

                    virtualFiles.Add(sqlDr["FileName"],sqlDr["FileData"]);

                }//while

            }//using

 

            return virtualFiles;

        }

 

        public string GetFileContents(string virPath)

        {

            //Obtain a file name from the virtual path. 

            string fileName = ExtractFileName(virPath);

 

            string fileContents = string.Empty;

 

            //Ater you obtain the file name, find it in the hashtable, and then

            //return the content for that file from the Values collection.    

            foreach (DictionaryEntry pair in virtualFiles)

            {

                if (pair.Key.Equals(fileName))

                {

                    fileContents = virtualFiles[fileName].ToString();

                }//if

            }//foreach

 

            return fileContents;

        }

 

        private string ExtractFileName(string virPath)

        {

            //Extract a file name from the virtual path and return it.

            return Path.GetFileName(virPath);

        }

 

        public bool CheckIfFileExists(string virPath)

        {

            string fileName = ExtractFileName(virPath);

 

            //After you extract the file name, find it in the hashtable of 

            //virtual files. If the file name is found, return true. Otherwise, return false.    

            foreach (DictionaryEntry pair in virtualFiles)

            {

 

                if (pair.Key.Equals(fileName))

                {

                    return true;

                }//if

            }//foreach

 

            return false;

 

        }

    }

}

In addition, these MSDN articles may also be useful to you in implementing the VirtualPathProvider in conjunction with CacheDependencies:
 


 

No comments:

Post a Comment