Click here to Skip to main content
15,868,340 members
Articles / Web Development / ASP.NET
Article

Creating Custom Virtual Path Provider in ASP.Net 2.0

Rate me:
Please Sign up or sign in to vote.
3.37/5 (17 votes)
21 Dec 20063 min read 87.2K   915   27   14
This article helps in understanding VirtualPathProvider in ASP.Net 2.0 and provides guidance in creating one.

Introduction

Virtual path provider in ASP.Net 2.0 is a way to make the world believe that something exists on the server which in reality is not. This enables the web application to retrieve resources from a virtual file system. This allows file systems containing browsable resources to be virtualized.

The virtual file system, including the folder and file hierarchy, is maintained in any source except for the file system. This alternate file system can be SQL Server or Zipped file or in obfuscated format. However, ASP.Net does not restrict using Windows File System, if somebody wants to use it.

Virtual file system can contain any file in the web application other than

  1. Binaries
  2. Web.config
  3. Global.asax
  4. Files in special folder such as App_Data and so on

All the above mentioned resources are loaded by asp.net before the virtual path provider and hence cannot be inside the virtual file system.

Moreover, if the site is precompiled, the content inside the virtual file system (the content provided by the virtualpathprovider) is not compiled

Creating a custom VirtualPathProvider

Why do we need a custom virtual path provider? Well, we need that to specify how to relate your virtual file with the request. Custom VirtualPathProvider is to convert the request from the user for the Virtual File and convert that to a request that can be understood by ASP.Net.

In the attached sample, I’m using text files as my source for asp.net markup and these files are placed inside the App_Code folder instead of the regular folder. To add little complexity around that, I’m creating the ASP.Net markup by replacing the placeholders specified in the text file with a valid asp.net markup.

In a real scenario, you might want to get his information from a database or some other place instead of a text file. Microsoft Office SharePoint uses this approach to store all the contents inside a Microsoft SQL Server instance.

First and foremost, we need to create a class by deriving the base VirtualPathProvider class. VirtualPathProvider base class implements several methods but is not tailor made for any situation. So we’ll have to override the methods that will fetch the required data for us, process the data (if required) and pass on this processed markup to the ASP.Net.

C#
public class TextVirtualPathProvider : VirtualPathProvider
{
  public static void AppInitialize()
  {
    ...
  }
  
  public override bool FileExists( string virtualPath )
  {
    ...
  }

  public override VirtualFile GetFile( string virtualPath )
  {
    ...
  }
}

In the code above, TextVirtualPathProvider is inheriting from VirtualPathProvider and overriding two methods FileExists() and GetFile() and defines a custom logic for processing

AppInitialize() is a special method. This is kind of Main() for the VirtualPathProvider and is invoked before anything else in this class.

Typically, this method registers this VirtualPathProvider with the ASP.Net Compilation system to specify an alternate execution path.

C#
HostingEnvironment.RegisterVirtualPathProvider(new TextVirtualPathProvider()); 

This method is not mandatory in all the CustomVirtualPathProviders. We can even specify the same code inside the Application_Start() even inside Global.asax file.

One more important method is the GetFile() method. This method returns a VirtualFile type. This method is invoked by the ASP.Net engine to get the markup for the virtual file.

The next step in creating the custom provider is to create a class that derives VirtualFile class that knows what exactly should be done for the virtual file request.

C#
public class TextFile : VirtualFile
{
  public TextFile( string virtualPath, TextVirtualPathProvider provider )
                 : base( virtualPath )
  {
    ...
  }

  public override Stream Open()
  {
    ...
  }
}

This class is pretty straightforward aswell, a constructor and an override method.

The constructor has to have virtualPath to initialize the base class object, the virtualpathprovider is provided to get any additional information and is purely optional.

ASP.Net engine, when obtained the handle to this object invokes the Open() method to get the stream containing the markup. So, Open() is the main method in the entire code that should understand the virtual file system and how to respond to any virtual file request.

Summary

This is fairly useful functionality in ASP.Net 2.0. Some of the very straight forward implementations of this would be to provide tiny urls for the requests. Instead of a request like http://www.myserver.com/articles.aspx?id=10, we can have http://www.myserver.com/articles/VirtualPathProvider.aspx which is not present physically, but fetched from the database. A more complex implementation would be that of a SharePoint portal sort of application.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Microsoft
India India
I'm working as a Consultant at Microsoft Global Services and like to spend my spare time investigating new stuff and write articles about the ones that are less discussed about. Please feel free to send your comments/suggestions/corrections on any of my work.

Comments and Discussions

 
QuestionFailed Load JS File directly in IIS Pin
HamidEtesami19-Jun-13 6:00
HamidEtesami19-Jun-13 6:00 
QuestionWeb.config applicationSettings Pin
Mark hafel23-Feb-13 11:09
Mark hafel23-Feb-13 11:09 
GeneralMy vote of 3 Pin
Scarophion25-Mar-11 6:50
Scarophion25-Mar-11 6:50 
GeneralVirtual Path in Web Application model Pin
Nadia Monalisa31-Aug-09 13:03
Nadia Monalisa31-Aug-09 13:03 
GeneralVPP with wildcard map on iss6 Pin
jameslock7-Nov-08 14:37
jameslock7-Nov-08 14:37 
QuestionWhy such a poor score? Pin
RobWhite17-May-07 23:25
RobWhite17-May-07 23:25 
AnswerRe: Why such a poor score? Pin
Sidhartha Gundavarapu17-May-07 23:36
Sidhartha Gundavarapu17-May-07 23:36 
QuestionChanging the content-type Pin
dugrless3-Mar-07 12:03
dugrless3-Mar-07 12:03 
AnswerRe: Changing the content-type Pin
meaningoflights13-Apr-08 21:47
meaningoflights13-Apr-08 21:47 
QuestionVirtualPathProvider and WebDAV? Pin
ingbabic5-Feb-07 0:42
ingbabic5-Feb-07 0:42 
AnswerRe: VirtualPathProvider and WebDAV? Pin
Sidhartha Gundavarapu5-Feb-07 0:51
Sidhartha Gundavarapu5-Feb-07 0:51 
QuestionStream cached? Pin
skitsanos13-Jan-07 10:46
skitsanos13-Jan-07 10:46 
AnswerRe: Stream cached? Pin
Sidhartha Gundavarapu5-Feb-07 0:49
Sidhartha Gundavarapu5-Feb-07 0:49 
AnswerRe: Stream cached? Pin
meaningoflights13-Apr-08 21:50
meaningoflights13-Apr-08 21:50 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.