ASP.NET读取DLL文件中的页面和用户控件(VirtualPathProvider VirtualFile)
来源:互联网 发布:k means聚类算法电商 编辑:程序博客网 时间:2024/06/11 14:30
举个例子,你的项目中所有的aspx页面均存放在Website这个目录下,那么通常你可以使用相对路径~/WebSite/WebPage.aspx来找到目录下的WebPage页面。
当你需要从某个程序集(DLL)中加载一个page到你的页面上来。通常情况下必须要使用Assembly.load()方法来找到它,并且很有可能需要使用到反射来确定它的类型,并且将他返回给页面,但是当你使用VirtualFile和VirtualPathProvider的时候,这时候对于父页面来说,它只需要去读取这个custom file的虚拟路径即可,具体返回的stream由你自己来定义(例如你仍然可以使用Assembly.load来获取文件)。那么你可以自定义它的路径来保证路径的一致性。
举个例子来看看如何来实现这个功能,能够更清晰一些。
准备工作是建一个Web应用程序,创建一个页面WebPage和一个用户控件WebUserControl,同理新建一个另外一个项目也包含同名的这两个文件,当然你最好在页面上区分它们来自本项目还是其他项目。例如本项目的写“I am from 本项目名”,其他项目则改为其他项目的名字。
[本示例完整源码下载(0分)] http://download.csdn.net/detail/aa466564931/4414671
首先我们创建一个CustomFile继承自VirtualFile,这个类的作用是加载来自其他程序集的文件并且返回给VirtualFileProvider使用:
public class CustomFile: VirtualFile { string path { get; set; } public CustomFile(string virtualPath) : base(virtualPath) { path = VirtualPathUtility.ToAppRelative(virtualPath); } /// <summary> /// Override Open method to load resource files of assembly. /// </summary> /// <returns></returns> public override System.IO.Stream Open() { string[] strs = path.Split('/'); string name = strs[2]; string resourceName = strs[3]; name = Path.Combine(HttpRuntime.BinDirectory, name); Assembly assembly = Assembly.LoadFile(name); if (assembly != null) { Stream s = assembly.GetManifestResourceStream(resourceName); return s; } else { return null; } } }
接着创建一个CustomVirtualProvider类继承自VirtualPathProvider,这里我们必须实现以下几个方法(Assembly是我自定义的文件名,你以为改为任意名称),注意AssemblyPathExist这个方法,在使用base.FileExists判断之前先进行我们的自定义路径的判断,如果存在则使用VirtualFile来取代默认的方法:
public class CustomPathProvider : VirtualPathProvider { public CustomPathProvider() { } /// <summary> /// Make a judgment that application find path contains specifical folder name. /// </summary> /// <param name="path"></param> /// <returns></returns> private bool AssemblyPathExist(string path) { string relateivePath = VirtualPathUtility.ToAppRelative(path); return relateivePath.StartsWith("~/Assembly/", StringComparison.InvariantCultureIgnoreCase); } /// <summary> /// If we can find this virtual path, return true. /// </summary> /// <param name="virtualPath"></param> /// <returns></returns> public override bool FileExists(string virtualPath) { if (this.AssemblyPathExist(virtualPath)) { return true; } else { return base.FileExists(virtualPath); } } /// <summary> /// Use custom VirtualFile class to load assembly resources. /// </summary> /// <param name="virtualPath"></param> /// <returns></returns> public override VirtualFile GetFile(string virtualPath) { if (AssemblyPathExist(virtualPath)) { return new CustomFile(virtualPath); } else { return base.GetFile(virtualPath); } } /// <summary> /// Return null when application use virtual file path. /// </summary> /// <param name="virtualPath"></param> /// <param name="virtualPathDependencies"></param> /// <param name="utcStart"></param> /// <returns></returns> public override CacheDependency GetCacheDependency(string virtualPath, IEnumerable virtualPathDependencies, DateTime utcStart) { if (AssemblyPathExist(virtualPath)) { return null; } else { return base.GetCacheDependency(virtualPath, virtualPathDependencies, utcStart); } } }
OK, 接下来我们就可以使用我们自定义的path来加载程序集中的文件了,我们把一下代码放在default页面中:
注意~/WebSite/WebUserControl.ascx是本项目的文件, ~/Assembly/CSASPNETAssembly.dll/CSASPNETAssembly.WebUserControl.ascx是来自于CSASPNETAssembly.dll的文件:
public partial class Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { // Add relative web pages and user controls in assembly and this application. DataTable tab = this.InitializeDataTable(); if (tab != null && tab.Rows.Count != 0) { for (int i = 0; i < tab.Rows.Count; i++) { Control control = Page.LoadControl(tab.Rows[i]["UserControlUrl"].ToString()); UserControl usercontrol = control as UserControl; Page.Controls.Add(usercontrol); HyperLink link = new HyperLink(); link.NavigateUrl = tab.Rows[i]["WebPageUrl"].ToString(); link.Text = tab.Rows[i]["WebPageText"].ToString(); Page.Controls.Add(link); } } } /// <summary> /// Initialize a DataTable variable for storing URL and text properties. /// </summary> /// <returns></returns> protected DataTable InitializeDataTable() { DataTable tab = new DataTable(); DataColumn userControlUrl = new DataColumn("UserControlUrl",Type.GetType("System.String")); tab.Columns.Add(userControlUrl); DataColumn webPageUrl = new DataColumn("WebPageUrl", Type.GetType("System.String")); tab.Columns.Add(webPageUrl); DataColumn webPageText = new DataColumn("WebPageText", Type.GetType("System.String")); tab.Columns.Add(webPageText); DataRow dr = tab.NewRow(); dr["UserControlUrl"] = "~/Assembly/CSASPNETAssembly.dll/CSASPNETAssembly.WebUserControl.ascx"; dr["WebPageUrl"] = "~/Assembly/CSASPNETAssembly.dll/CSASPNETAssembly.WebPage.aspx"; dr["WebPageText"] = "Assembly/WebPage"; DataRow drWebSite = tab.NewRow(); drWebSite["UserControlUrl"] = "~/WebSite/WebUserControl.ascx"; drWebSite["WebPageUrl"] = "~/WebSite/WebPage.aspx"; drWebSite["WebPageText"] = "WebSite/WebPage"; tab.Rows.Add(dr); tab.Rows.Add(drWebSite); return tab; } }
- ASP.NET读取DLL文件中的页面和用户控件(VirtualPathProvider VirtualFile)
- ASP.NET中的用户控件和用户自定义控件
- ASP.NET中用户控件和页面的区别
- ASP.NET用户控件操作ASPX页面(在ASPX页面捕捉用户控件的事件)
- ASP.NET 页面添加用户控件
- ASP.NET页面引入用户控件
- ASP.NET读取页面请求中的参数
- asp.net 调用用户控件中的方法
- Asp.net利用事件委托,实现用户控件中的行为触发所在页面的处理函数
- asp.net 将ascx用户控件打包成dll
- Asp.net用户控件和委托事件
- ASP.NET用户控件
- asp.net用户控件
- asp.net ajax 和 asp.net 2.0中的fileupload合力打造无刷新文件上传控件
- asp.net ajax 和 asp.net 2.0中的fileupload合力打造无刷新文件上传控件
- asp.net页面装载用户控件时的事件顺序
- ASP.NET 中页面获取所用用户控件的事件
- ASP.NET动态加载用户控件的页面生成过程
- PhysX物理引擎(编程入门)
- JSON的基本语法 .
- 从ramdisk根文件系统启动Linux成功
- WCF gSoap 互相调用(主动调用、非回调)
- apache OFBiz的安装
- ASP.NET读取DLL文件中的页面和用户控件(VirtualPathProvider VirtualFile)
- 游戏开发中常用的设计模式
- linux软件开发常见软件安装--jdk jboss aciveMQ
- SVN常用命令
- 第一次刷机,MOTO MB300 平刷2.3
- wxPython中使用剪贴板存取文本数据
- Android系统默认值的设置
- 2012.07.05
- 游戏中的资源管理----资源高速缓存