ASP. NET Virtual File System

Source: Internet
Author: User

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:

 
 
  1. DllVirtualPathProvider  
  2. public class DllVirtualPathProvider : System.Web.Hosting.VirtualPathProvider  
  3. {  
  4. public DllVirtualPathProvider()  
  5. {  
  6. }  
  7.  
  8. public override string CombineVirtualPaths(string basePath, string relativePath)  
  9. {  
  10. if (IsAppResourcePath(basePath))  
  11. {  
  12. return null;    
  13. }  
  14.  
  15. return Previous.CombineVirtualPaths(basePath, relativePath);    
  16. }  
  17.  
  18. public override System.Runtime.Remoting.ObjRef CreateObjRef(Type requestedType)  
  19. {  
  20. return Previous.CreateObjRef(requestedType);    
  21. }  
  22.  
  23. public override bool DirectoryExists(string virtualDir)  
  24. {  
  25. if (IsAppResourcePath(virtualDir))  
  26. {  
  27. return true;    
  28. }  
  29. else  
  30. {  
  31. return Previous.DirectoryExists(virtualDir);    
  32. }  
  33.  
  34. }  
  35.  
  36. public override string GetCacheKey(string virtualPath)  
  37. {  
  38. if (IsAppResourcePath(virtualPath))  
  39. {  
  40. return null;    
  41. }  
  42. else  
  43. {  
  44. return Previous.GetCacheKey(virtualPath);    
  45. }  
  46. }  
  47.  
  48. public override string GetFileHash(string virtualPath, 
    IEnumerable virtualPathDependencies)  
  49. {  
  50. if (IsAppResourcePath(virtualPath))  
  51. {  
  52. return null;    
  53. }  
  54. else  
  55. {  
  56. return Previous.GetFileHash(virtualPath, virtualPathDependencies);    
  57. }  
  58. }  
  59.  
  60. private bool IsAppResourcePath(string virtualPath)  
  61. {  
  62. String checkPath = VirtualPathUtility.ToAppRelative(virtualPath);    
  63. return checkPath.StartsWith("~/MyUserControl/Test.Control.dll/", 
    StringComparison.InvariantCultureIgnoreCase);    
  64. }  
  65.  
  66. public override bool FileExists(string virtualPath)  
  67. {  
  68. return (IsAppResourcePath(virtualPath) || Previous.FileExists(virtualPath));    
  69. }  
  70.  
  71. public override VirtualFile GetFile(string virtualPath)  
  72. {  
  73. if (IsAppResourcePath(virtualPath))  
  74. {  
  75. return new AssemblyResourceVirtualFile(virtualPath);    
  76. }  
  77. else  
  78. {  
  79. return Previous.GetFile(virtualPath);    
  80. }  
  81. }  
  82.  
  83. public override System.Web.Caching.CacheDependency GetCacheDependency(string virtualPath,  
  84. System.Collections.IEnumerable virtualPathDependencies, DateTime utcStart)  
  85. {  
  86. if (IsAppResourcePath(virtualPath))  
  87. {  
  88. string path = HttpRuntime.AppDomainAppPath + virtualPath.Substring(1);    
  89.  
  90. return new System.Web.Caching.CacheDependency(path);    
  91. }  
  92. else  
  93. {  
  94. return Previous.GetCacheDependency(virtualPath, virtualPathDependencies, utcStart);    
  95. }  
  96. }  
  1. Analysis of configuration files in ASP. NET
  2. . Net ria Services is as convenient as ASP. NET
  3. Overview of the UpdatePanel control in ASP. net ajax Extensions
  4. ASP. NET calls the UpdatePanel Update () method
  5. Introduction to the ASP. net ajax wcf Service

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.