ASP.NET虚拟文件系统的作用

发布时间:2021-09-18 02:00:54 作者:chen
来源:亿速云 阅读:131

这篇文章主要讲解了“ASP.NET虚拟文件系统的作用”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“ASP.NET虚拟文件系统的作用”吧!

具体实现步骤如下:

一、开发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. }  

感谢各位的阅读,以上就是“ASP.NET虚拟文件系统的作用”的内容了,经过本文的学习后,相信大家对ASP.NET虚拟文件系统的作用这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是亿速云,小编将为大家推送更多相关知识点的文章,欢迎关注!

推荐阅读:
  1. asp.net 反射的作用是什么
  2. 怎么理解Linux虚拟文件系统

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

asp.net

上一篇:Ceph的核心组件及概念介绍

下一篇:NetBeans连接数据库和使用JSTL的步骤

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》