ASP.NET虚拟文件系统

开发 后端
本文以一个例子来说明ASP.NET虚拟文件系统的使用,要实现的功能场景描述,以及介绍开发web用户控件和开发一个虚拟文件系统提供类。

在Asp.Net的开发过程中页面文件等都是放在当前网站目录下的,其实我们可以利用.Net2.0新增的ASP.NET虚拟文件系统(VirtualPathProvider)将页面、图片等信息保存到数据库或其他目录中去,达到灵活配置。

本文以一个例子来说明ASP.NET虚拟文件系统的使用,要实现的功能场景描述如下:
以前开发Asp.Net的web用户控件时,需要把用户控件和当前项目作为同一个项目时才能正常使用,而且发布时需要把dll文件和所有的ascx文件都发布才能使用;另外也不方便作为公用类给其他人使用
ASP.NET虚拟文件系统后可以把ascx文件作为资源打包到dll中,下次只要有这个dll就可以使用了,不需要ascx文件,很方便。

具体实现步骤如下:

一、开发web用户控件

这一步和以前的开发没有区别。
1、首先新建一个web应用程序(需要VS2005 sp1支持)
2、然后在里面开发几个web用户控件
3、在ascx文件上右键-〉属性-〉生成操作选择嵌入的资源
4、生成dll就可以了(dll的名字为:Test.Control.dll,后面会用到)

二、开发一个虚拟文件系统提供类

这一步是最重要的一步。

具体思路就是:在系统中注册这个类,然后在每访问一个文件/资源的时候会自动调用这个类,在这个类中判断文件的路径是否是我们定义的,如果是就用我们的逻辑来处理,即从dll中取出资源。

首先把类的代码贴出来,我想可能许多人应该和我一样,喜欢直接先看代码:

 

  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. 浅析ASP.NET中配置文件
  2. .NET RIA Services就像ASP.NET那样方便
  3. ASP.NET AJAX Extensions中UpdatePanel控件概述
  4. ASP.NET调用UpdatePanel的Update()方法
  5. 浅谈ASP.NET AJAX的WCF服务
责任编辑:佚名 来源: IT专家网
相关推荐

2020-07-22 14:53:06

Linux系统虚拟文件

2009-07-27 15:51:49

ASP.NET虚拟主机

2018-08-24 10:10:25

Linux文件系统技术

2019-09-20 10:04:45

Linux系统虚拟文件

2019-05-29 16:33:32

Linux虚拟系统

2019-05-22 09:00:16

Linux虚拟文件系统

2023-12-06 09:32:35

Linux系统

2024-02-02 10:38:06

虚拟文件系统VFS

2020-10-12 17:40:44

lsofLinux虚拟文件

2022-04-21 14:09:17

lsofLinux虚拟文件

2010-03-02 14:21:30

Linux操作系统

2017-02-06 16:18:57

微软GitGVFS

2019-06-14 14:58:58

虚拟文件系统Linux

2009-07-22 17:45:35

ASP.NET教程

2009-07-28 17:17:19

ASP.NET概述

2009-08-03 14:22:33

什么是ASP.NET

2009-07-21 15:34:32

ASP.NET 2.0

2022-01-14 08:39:47

鸿蒙HarmonyOS应用

2009-08-05 18:10:09

ASP.NET Def

2009-07-27 12:22:03

ASP.NET和ASPASP.NET入门教程
点赞
收藏

51CTO技术栈公众号