在Asp.Net的開(kāi)發(fā)過(guò)程中頁(yè)面文件等都是放在當(dāng)前網(wǎng)站目錄下的,其實(shí)我們可以利用.Net2.0新增的虛擬文件系統(tǒng)(VirtualPathProvider)將頁(yè)面、圖片等信息保存到數(shù)據(jù)庫(kù)或其他目錄中去,達(dá)到靈活配置。
以前開(kāi)發(fā)Asp.Net的web用戶控件時(shí),需要把用戶控件和當(dāng)前項(xiàng)目作為同一個(gè)項(xiàng)目時(shí)才能正常使用,而且發(fā)布時(shí)需要把dll文件和所有的ascx文件都發(fā)布才能使用;另外也不方便作為公用類(lèi)給其他人使用 利用虛擬文件系統(tǒng)后可以把a(bǔ)scx文件作為資源打包到dll中,下次只要有這個(gè)dll就可以使用了,不需要ascx文件,很方便。 具體實(shí)現(xiàn)步驟如下: 一、開(kāi)發(fā)web用戶控件 這一步和以前的開(kāi)發(fā)沒(méi)有區(qū)別。 1、首先新建一個(gè)web應(yīng)用程序(需要VS2005 sp1支持) 2、然后在里面開(kāi)發(fā)幾個(gè)web用戶控件 3、在ascx文件上右鍵-〉屬性-〉生成操作選擇嵌入的資源 4、生成dll就可以了(dll的名字為:Test.Control.dll,后面會(huì)用到) 二、開(kāi)發(fā)一個(gè)虛擬文件系統(tǒng)提供類(lèi) 這一步是最重要的一步。 具體思路就是:在系統(tǒng)中注冊(cè)這個(gè)類(lèi),然后在每訪問(wèn)一個(gè)文件/資源的時(shí)候會(huì)自動(dòng)調(diào)用這個(gè)類(lèi),在這個(gè)類(lèi)中判斷文件的路徑是否是我們定義的,如果是就用我們的邏輯來(lái)處理,即從dll中取出資源。 首先把類(lèi)的代碼貼出來(lái),我想可能許多人應(yīng)該和我一樣,喜歡直接先看代碼:) 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); } } }重點(diǎn)有以下幾個(gè): 1、必須從VirtualPathProvider類(lèi)繼承 2、IsAppResourcePath方法是用來(lái)判斷是否為我們定義的路徑格式:~/MyUserControl/Test.Control.dll/,下面調(diào)用的時(shí)候就使用這個(gè)路徑 3、注意GetCacheKey方法: public override string GetCacheKey(string virtualPath) { if (IsAppResourcePath(virtualPath)) { return null; } else { return Previous.GetCacheKey(virtualPath); } }這里當(dāng)符合條件時(shí)一定要返回null,如果返回"",會(huì)導(dǎo)致所有的用戶控件都使用一個(gè)key值,從而所有的用戶控件都顯示同樣的內(nèi)容了。如果返回其他非空字符,會(huì)報(bào)異常(它會(huì)去查找cache,我們是沒(méi)有的) 另外所有的方法當(dāng)不符合我們的條件時(shí)一定要調(diào)用 Previous.**** 因?yàn)橄到y(tǒng)中可能有多個(gè)虛擬文件提供程序的。
4、GetCacheDependency方法: if (IsAppResourcePath(virtualPath)) { string path = HttpRuntime.AppDomainAppPath + virtualPath.Substring(1);
return new System.Web.Caching.CacheDependency(path); }這個(gè)方法是用來(lái)決定Cache的使用的,如果返回null,會(huì)導(dǎo)致性能?chē)?yán)重下降,每次調(diào)用用戶控件時(shí)都會(huì)重新編譯,這里返回的當(dāng)前路徑(需要在網(wǎng)站目錄下再建立子目錄:MyUserControl\Test.Control.dll),這個(gè)目錄下是空的,這樣當(dāng)每次取Cache時(shí)都會(huì)認(rèn)為我們的ascx沒(méi)有修改,不需要重新編譯。
5、在GetFile方法中我們返回的是一個(gè)AssemblyResourceVirtualFile類(lèi): class AssemblyResourceVirtualFile : VirtualFile { string path; public AssemblyResourceVirtualFile(string virtualPath) : base(virtualPath) { path = VirtualPathUtility.ToAppRelative(virtualPath); }
public override System.IO.Stream Open() { string[] parts = path.Split('/'); string assemblyName = parts[2]; string resourceName = parts[3]; assemblyName = Path.Combine(HttpRuntime.BinDirectory, assemblyName); System.Reflection.Assembly assembly = System.Reflection.Assembly.LoadFrom(assemblyName); if (assembly != null) { return assembly.GetManifestResourceStream(resourceName); } return null; } }這個(gè)類(lèi)的目的就是從我們的dll中讀出資源文件。 三、注冊(cè)這個(gè)虛擬文件提供程序 這一個(gè)很簡(jiǎn)單,在global.asax中注冊(cè): protected void Application_Start(object sender, EventArgs e) { System.Web.Hosting.HostingEnvironment.RegisterVirtualPathProvider(new DllVirtualPathProvider()); } 四、調(diào)用dll中的用戶控件 Control control1 = this.LoadControl("/MyUserControl/Test.Control.dll/Test.Control.Sample.List.ascx"); Control control2 = this.LoadControl("/MyUserControl/Test.Control.dll/Test.Control.Sample.Sample.ascx");
form1.Controls.Add(control1); form1.Controls.Add(control2);比較簡(jiǎn)單,就是路徑要和前面的一致。 后記: 首先感謝 Leepy 同志認(rèn)真踏實(shí)的精神,找出了我這篇文章中的一個(gè)BUG。 原來(lái)我的第四步:調(diào)用dll中的用戶控件,是一個(gè)web應(yīng)用程序,如果是一個(gè)web站點(diǎn)的話就會(huì)出現(xiàn)問(wèn)題。因?yàn)樵赩S2005中調(diào)試時(shí)這兩種方式呈現(xiàn)出來(lái)的Url是不一樣的: web應(yīng)用程序:http://localhost:****/Default.aspx web站點(diǎn) :http://localhost:****/WebSite1/Default.aspx 也就是說(shuō)我原來(lái)的程序沒(méi)有考慮到非根目錄部署的情況,針對(duì)這個(gè)BUG要修改的地方有(為了BUG的原始記錄,我就不在原文中修改了,把修改點(diǎn)列在下面): 1、GetCacheDependency方法: GetCacheDependency public override System.Web.Caching.CacheDependency GetCacheDependency(string virtualPath, System.Collections.IEnumerable virtualPathDependencies, DateTime utcStart) { if (IsAppResourcePath(virtualPath)) { string path = HttpRuntime.BinDirectory;
return new System.Web.Caching.CacheDependency(path); } else { return Previous.GetCacheDependency(virtualPath, virtualPathDependencies, utcStart); } }這里改成直接改成當(dāng)前網(wǎng)站的bin目錄還可以省掉建空目錄的步驟了, 軟件開(kāi)發(fā)網(wǎng)
2、在調(diào)用的地方修改成以下方式: Control control1 = this.LoadControl("~/MyUserControl/MyWebApplication.dll/MyWebApplication.Sample1.ascx"); Control control2 = this.LoadControl("~/MyUserControl/MyWebApplication.dll/MyWebApplication.Sample2.ascx"); form1.Controls.Add(control1); form1.Controls.Add(control2);
|