The VirtualPathProvider class enables you to abstract the pages in a web application from the file system. In other words, it enables you to store your ASP.NET pages.e.g, The VirtualPathProvider class to store all the pages in your application in a database. This would be an appropriate choice when you need to build a CMS. If you store pages in a database, then users can update the pages easily in an application through an HTML form interface and save the changes to the database.of the VirtualPathProvider Class Unfortunately, you can’t use the VirtualPathProvider with every type of file.
In particular,the following types of files must always be located on the file system:
. Global.asax file
. Web.Config files
. App_Data folder
. App_Code folder
. App_GlobalResources folder
. App_LocalResource folders
. Bin folder
Every other type of file is fair game. This includes ASP.NET pages, User Controls, Themes,and Master Pages.
Understanding the VirtualPathProvider Class The VirtualPathProvider class is a MustInherit (abstract) class. It contains the following methods, which you can override:
VirtualPathProvider needs to know when a file has been modified so that it can retrieve the new version of the file and compile it. By default, the ASP.NET Framework uses a file dependency to determine when a file has been modified on the hard drive. However, in this situation a SqlCacheDependency is used because the files will be stored in a database.
VirtualPathProvider also includes a very useful property:
The GetFile() method returns an instance of the VirtualFile class. When using the VirtualPathProvider, you must create a new class that inherits from the VirtualFile class. This class contains the following properties:
The GetDirectory() method returns an instance of the VirtualDirectory class. This class contains the following properties:
In particular,the following types of files must always be located on the file system:
. Global.asax file
. Web.Config files
. App_Data folder
. App_Code folder
. App_GlobalResources folder
. App_LocalResource folders
. Bin folder
Every other type of file is fair game. This includes ASP.NET pages, User Controls, Themes,and Master Pages.
Understanding the VirtualPathProvider Class The VirtualPathProvider class is a MustInherit (abstract) class. It contains the following methods, which you can override:
- CombineVirtualPaths() : Returns a combined path from two paths.
- DirectoryExists() : Returns true when a directory exists.
- FileExists() : Returns true when a file exists.
- GetCacheDependency() : Returns a cache dependency object that indicates when a file has been changed.
- GetCacheKey() : Returns the key used by the cache dependency.
- GetDirectory() : Returns a VirtualDirectory.
- GetFile() : Returns a VirtualFile.
- GetFileHash() : Returns a hash of the files used by the cache dependency.
- OpenFile() : Returns the contents of a file.
VirtualPathProvider needs to know when a file has been modified so that it can retrieve the new version of the file and compile it. By default, the ASP.NET Framework uses a file dependency to determine when a file has been modified on the hard drive. However, in this situation a SqlCacheDependency is used because the files will be stored in a database.
VirtualPathProvider also includes a very useful property:
- Previous : Returns the previously registered VirtualPathProvider.
The GetFile() method returns an instance of the VirtualFile class. When using the VirtualPathProvider, you must create a new class that inherits from the VirtualFile class. This class contains the following properties:
- IsDirectory : Always returns False.
- Name : Returns the name of the file.
- VirtualPath : Returns the virtual path of the file.
- Open() : Returns the contents of the file.
The GetDirectory() method returns an instance of the VirtualDirectory class. This class contains the following properties:
- Children : Returns all the files and directories that are children of the current directory.
- Directories : Returns all the directories that are children of the current directory.
- Files : Returns all the files that are children of the current directory.
- IsDirectory : Always returns True.
- Name : Returns the name of the directory.
- VirtualPath : Returns the virtual path of the directory.
- AppendTrailingSlash() : Returns a path with at most one forward slash appended to the end of the path.
- Combine() : Returns the combination of two virtual paths.
- GetDirectory() : Returns the directory portion of a path.
- GetExtension() : Returns the file extension of a path.
- GetFileName() : Returns the file name from a path.
- IsAbsolute() : Returns True when a path starts with a forward slash.
- IsAppRelative() : Returns True when a path starts with a tilde (~).
- MakeRelative() : Returns a relative path from an application-relative path.
- RemoveTrailingSlash() : Removes trailing slash from the end of a path.
- ToAbsolute() : Returns a path that starts with a forward slash.
- ToAppRelative() : Returns a path that starts with a tilde (~).