Click here to Skip to main content
Click here to Skip to main content

Creating Custom Virtual Path Provider in ASP.Net 2.0

, 21 Dec 2006
Rate this:
Please Sign up or sign in to vote.
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.

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.

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.

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

Share

About the Author

Sidhartha Gundavarapu
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 PinmemberHamidEtesami19-Jun-13 7:00 
QuestionWeb.config applicationSettings PinmemberMark hafel23-Feb-13 12:09 
GeneralMy vote of 3 PinmemberScarophion25-Mar-11 7:50 
GeneralVirtual Path in Web Application model PinmemberEmran Hussain31-Aug-09 14:03 
GeneralVPP with wildcard map on iss6 Pinmemberjimloko7-Nov-08 15:37 
QuestionWhy such a poor score? PinmemberRobWhite18-May-07 0:25 
Why has this article got such a poor score? It's a clear sample of achieving something that is not immediately transparent in the docs.
 
Thanks, a really useful article. Big Grin | :-D
 
Rob White author of http://www.jacquelinewhite.co.uk[^]
AnswerRe: Why such a poor score? PinmemberSidhartha Gundavarapu18-May-07 0:36 
QuestionChanging the content-type Pinmemberdugrless3-Mar-07 13:03 
AnswerRe: Changing the content-type Pinmembermeaningoflights13-Apr-08 22:47 
QuestionVirtualPathProvider and WebDAV? Pinmemberingbabic5-Feb-07 1:42 
AnswerRe: VirtualPathProvider and WebDAV? PinmemberSidhartha Gundavarapu5-Feb-07 1:51 
QuestionStream cached? Pinmemberarachno13-Jan-07 11:46 
AnswerRe: Stream cached? PinmemberSidhartha Gundavarapu5-Feb-07 1:49 
AnswerRe: Stream cached? Pinmembermeaningoflights13-Apr-08 22:50 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.141216.1 | Last Updated 21 Dec 2006
Article Copyright 2006 by Sidhartha Gundavarapu
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid