Provides a set of methods that enable a Web application to retrieve resources from a virtual file system.
Assembly: System.Web (in System.Web.dll)
Thetype exposes the following members.
|CombineVirtualPaths||Combines a base path with a relative path to return a complete path to a virtual resource.|
|CreateObjRef||Creates an object that contains all the relevant information required to generate a proxy used to communicate with a remote object. (Inherited from MarshalByRefObject.)|
|DirectoryExists||Gets a value that indicates whether a directory exists in the virtual file system.|
|Equals(Object)||Determines whether the specified Object is equal to the current Object. (Inherited from Object.)|
|FileExists||Gets a value that indicates whether a file exists in the virtual file system.|
|Finalize||Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from Object.)|
|GetCacheDependency||Creates a cache dependency based on the specified virtual paths.|
|GetCacheKey||Returns a cache key to use for the specified virtual path.|
|GetDirectory||Gets a virtual directory from the virtual file system.|
|GetFile||Gets a virtual file from the virtual file system.|
|GetFileHash||Returns a hash of the specified virtual paths.|
|GetHashCode||Serves as a hash function for a particular type. (Inherited from Object.)|
|GetLifetimeService||Retrieves the current lifetime service object that controls the lifetime policy for this instance. (Inherited from MarshalByRefObject.)|
|GetType||Gets the Type of the current instance. (Inherited from Object.)|
|Initialize||Initializes the instance.|
|InitializeLifetimeService||Gives the object an infinite lifetime by preventing a lease from being created. (Overrides MarshalByRefObject.InitializeLifetimeService().)|
|MemberwiseClone()||Creates a shallow copy of the current Object. (Inherited from Object.)|
|MemberwiseClone(Boolean)||Creates a shallow copy of the current MarshalByRefObject object. (Inherited from MarshalByRefObject.)|
|OpenFile||Returns a stream from a virtual file.|
|ToString||Returns a string that represents the current object. (Inherited from Object.)|
The class provides a set of methods for implementing a virtual file system for a Web application. In a virtual file system, the files and directories are managed by a data store other than the file system provided by the server's operating system. For example, you can use a virtual file system to store content in a SQL Server database.
You can store any file that is processed on request in a virtual file system. This includes:
ASP.NET pages, master pages, user controls, and other objects.
Standard Web pages with extensions such as .htm and .jpg.
Any custom extension mapped to a BuildProvider instance.
Any named theme in the App_Theme folder.
You cannot store ASP.NET application folders or files that generate application-level assemblies in a virtual file system. This includes:
The Global.asax file.
Site map data files used by the XmlSiteMapProvider.
Directories that contain application assemblies or that generate application assemblies: Bin, App_Code, App_GlobalResources, any App_LocalResources.
The application data folder, App_Data.
If a Web site is precompiled for deployment, content provided by a instance is not compiled, and no instances are used by the precompiled site.
Registering a VirtualPathProvider
A custom instance should be registered with the ASP.NET compilation system by using the HostingEnvironment.RegisterVirtualPathProvider method before any page parsing or compilation is performed by the Web application.
Typically, a instance is registered in an AppInitialize method defined in the App_Code directory, or during the Application_Start event in the Global.asax file. For an example of registering a instance in an AppInitialize method, see the Example section.
You can register a instance during other events, but pages compiled and cached before the instance is registered will not be invalidated, even if the new instance would now provide the source for the previously compiled page.
When you inherit from , you must override the following members:
If your custom class supports directories in the virtual file system, you must override the following members.
If your virtual file system will contain themes for the Web site (by creating a virtual App_Themes directory), your custom class must support directories.
A custom class works with classes derived from the VirtualFile and VirtualDirectory classes. You should implement derived classes from these types to provide file and directory information from your virtual file system. For an example of a custom VirtualFile implementation, see the Example section of the VirtualFile class overview topic. For an example of a custom VirtualDirectory implementation, see the Example section of the VirtualDirectory class overview topic.
The following code example is a class implementation that creates a virtual file system using information stored in a DataSet object. The code example works with the code examples for the VirtualFile and VirtualDirectory classes to provide virtual resources from a data store that is loaded into a DataSet object.
This example has four parts: the class implementation, an XML data file used to populate the DataSet object, an AppStart object that contains an AppInitialize method used to register the class with the compilation system, and an ASP.NET page that provides links to the virtual files.
To use this sample code in an application, follow these steps.
Create a sample application on your Web server.
Copy the source code for the custom object (see below) into a file in the application's App_Code directory.
Copy the source code for the AppStart object (see below) into a file in the application's App_Code directory.
Copy the XML data (see below) into a file named XMLData.xml into a file in the application's App_Data directory.
Copy the default.aspx file (see below) into the root directory of the sample application. Use a Web browser to open the default.aspx file, and then click the links on the page to see the contents of the virtual files.
The first example is a custom class. The DirectoryExists and FileExists methods are overridden to indicate whether a requested directory is present in the virtual file system. The GetDirectory and GetFile methods are overridden to return custom VirtualDirectory and VirtualFile instances containing information from the virtual file system.
The class also provides a GetVirtualData method used by the VirtualDirectory and VirtualFile classes to access the DataSet object containing the virtual file system data. In a production implementation, this method would typically be implemented in a business object responsible for interacting with the data store.
The second example is the XML data file used to populate the DataSet object returned by the custom object. This XML data is used to demonstrate using the , VirtualDirectory, and VirtualFile objects to retrieve data from external data and is not intended to represent a production-quality data store.
<?xml version="1.0" encoding="utf-8" ?> <resource type="dir" path="/vrDir" parentPath="" content=""> <resource type="file" path="/vrDir/Level1FileA.vrf" parentPath="/vrDir" content="This is the content of file Level1FileA."> </resource> <resource type="file" path="/vrDir/Level1FileB.vrf" parentPath="/vrDir" content="This is the content of file Level1FileB."> </resource> <resource type="dir" path="/vrDir/Level2DirA" parentPath="/vrDir" content=""> <resource type="file" path="/vrDir/Level2DirA/Level2FileA.vrf" parentPath="/vrDir/Level2DirA" content="This is the content of file Level2FileA."> </resource> <resource type="file" path="/vrDir/Level2DirA/Level2FileB.vrf" parentPath="/vrDir/Level2DirA" content="This is the content of file Level2FileB."> </resource> </resource> <resource type="dir" path="/vrDir/Level2DirB" parentPath="/vrDir" content=""> <resource type="file" path="/vrDir/Level2DirB/Level2FileA.vrf" parentPath="/vrDir/Level2DirB" content="This is the content of file Level2FileA."> </resource> <resource type="file" path="/vrDir/Level2DirB/Level2FileB.vrf" parentPath="/vrDir/Level2DirB" content="This is the content of file Level2FileB."> </resource> </resource> </resource>
The third example provides an AppStart object that contains an AppInitialize method. This method is called during the initialization of an ASP.NET application to perform any custom initialization required. In this case, it registers the custom object with the ASP.NET build system.
The last example is an ASP.NET page that contains links to the virtual files contained in the virtual file system.
Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2