In Asp. in the development process of Net, the page files are stored in the current website directory. In fact, we can use. ASP.. NET Virtual File System (VirtualPathProvider) saves pages, images, and other information to the database or other directories for flexible configuration.
This article uses an example to describe the use of the ASP. NET Virtual File System. The functional scenarios to be implemented are described as follows:
Previously developed Asp. net web user control, you need to take the user control and the current project as the same project can be used normally, and the dll file and all ascx files must be released before use; in addition, it is not convenient to use it as a public class for others.
After ASP. NET Virtual File System, You can package the ascx file as a resource to the dll. Next time you have this dll, you can use it without the need for the ascx file, which is very convenient.
The specific implementation steps are as follows:
1. Develop web User Controls
This step is no different from previous development.
1. Create a web application (supported by VS2005 sp1)
2. Then develop several web User Controls
3. Right-click the ascx file-> properties-> Generate an operation and select an embedded resource.
4. Generate the dll (the dll name is Test. Control. dll, which will be used later)
2. Develop a virtual file system offering class
This step is the most important step.
The specific idea is: register this class in the system, and then automatically call this class every time you access a file/resource. In this class, you can determine whether the file path is defined by us, if yes, we will use our logic to process it, that is, to extract resources from the dll.
First, paste the class code. I think many people should look at the Code directly like me:
- DllVirtualPathProvider
- public class DllVirtualPathProvider : System.Web.Hosting.VirtualPathProvider
- {
- public DllVirtualPathProvider()
- {
- }
-
- public override string CombineVirtualPaths(string basePath, string relativePath)
- {
- if (IsAppResourcePath(basePath))
- {
- return null;
- }
-
- return Previous.CombineVirtualPaths(basePath, relativePath);
- }
-
- public override System.Runtime.Remoting.ObjRef CreateObjRef(Type requestedType)
- {
- return Previous.CreateObjRef(requestedType);
- }
-
- public override bool DirectoryExists(string virtualDir)
- {
- if (IsAppResourcePath(virtualDir))
- {
- return true;
- }
- else
- {
- return Previous.DirectoryExists(virtualDir);
- }
-
- }
-
- public override string GetCacheKey(string virtualPath)
- {
- if (IsAppResourcePath(virtualPath))
- {
- return null;
- }
- else
- {
- return Previous.GetCacheKey(virtualPath);
- }
- }
-
- public override string GetFileHash(string virtualPath,
IEnumerable virtualPathDependencies)
- {
- if (IsAppResourcePath(virtualPath))
- {
- return null;
- }
- else
- {
- return Previous.GetFileHash(virtualPath, virtualPathDependencies);
- }
- }
-
- private bool IsAppResourcePath(string virtualPath)
- {
- String checkPath = VirtualPathUtility.ToAppRelative(virtualPath);
- return checkPath.StartsWith("~/MyUserControl/Test.Control.dll/",
StringComparison.InvariantCultureIgnoreCase);
- }
-
- public override bool FileExists(string virtualPath)
- {
- return (IsAppResourcePath(virtualPath) || Previous.FileExists(virtualPath));
- }
-
- public override VirtualFile GetFile(string virtualPath)
- {
- if (IsAppResourcePath(virtualPath))
- {
- return new AssemblyResourceVirtualFile(virtualPath);
- }
- else
- {
- return Previous.GetFile(virtualPath);
- }
- }
-
- public override System.Web.Caching.CacheDependency GetCacheDependency(string virtualPath,
- System.Collections.IEnumerable virtualPathDependencies, DateTime utcStart)
- {
- if (IsAppResourcePath(virtualPath))
- {
- string path = HttpRuntime.AppDomainAppPath + virtualPath.Substring(1);
-
- return new System.Web.Caching.CacheDependency(path);
- }
- else
- {
- return Previous.GetCacheDependency(virtualPath, virtualPathDependencies, utcStart);
- }
- }
- }
- Analysis of configuration files in ASP. NET
- . Net ria Services is as convenient as ASP. NET
- Overview of the UpdatePanel control in ASP. net ajax Extensions
- ASP. NET calls the UpdatePanel Update () method
- Introduction to the ASP. net ajax wcf Service