ASP.NET VirtualPathProvider (上)

來源:互聯網
上載者:User
在某些特殊的情況下,網站常規的檔案、目錄組織方式不能滿足要求,在Asp.net 2.0提供了一些類來實現自己的虛擬檔案系統,可以把網站檔案儲存在Zip檔案裡、儲存在自己定製格式的二進位檔案中甚至資料庫中。這些類在System.Web.Hosting命名空間內定義,如:VirtualPathProvider、VirtualFile、VirtualDirectory等等。

以前實現頁面或檔案隱藏,可以繼承IHttpHandler,IHttpModule,然後在web.comfig檔案中配置,與之相比,VirtualPathProvider更強大。

可以映射到儲在虛擬檔案系統中的檔案或檔案夾包括:

ASP.NET 頁、主版頁面、使用者控制項以及其他對象。
具有 .htm 和 .jpg 這樣的副檔名的標準網頁。
映射到 BuildProvider 執行個體的任何自訂副檔名。
App_Theme 檔案夾中的任何命名主題。
 

但虛擬檔案系統中不能儲存下面的檔案夾或檔案:

Global.asax 檔案。
Web.config 檔案。
供 XmlSiteMapProvider 使用的網站地圖資料檔案。
包含或產生應用程式程式集的目錄:Bin、App_Code、App_GlobalResources 以及任何 App_LocalResources 目錄。
應用程式資料檔案夾 App_Data。
 

1.定製自己的虛擬路徑提供者
繼承VirtualPathProvider類。using System.IO;
using System.Web;
using System.Web.Hosting;
using System.Text;

namespace Xianfen.Net.MyVirtualPathProvider_File
{
    public class MyVirtualPathProvider : VirtualPathProvider
    {
        public override bool FileExists(string virtualPath)
        {
            virtualPath = virtualPath.ToLower();

            if (virtualPath.EndsWith("myvirtualfile.aspx"))
            {
                return true;
            }
            else
            {
                return Previous.FileExists(virtualPath);
            }
        }

        public override VirtualFile GetFile(string virtualPath)
        {
            virtualPath = virtualPath.ToLower();

            if (virtualPath.EndsWith("myvirtualfile.aspx"))
            {
                return new MyVirtualFile(virtualPath);
            }
            else
            {
                return Previous.GetFile(virtualPath);
            }
        }
    }
}

2.定製自己的虛擬檔案
繼承VirtualFile類。namespace Xianfen.Net.MyVirtualPathProvider_File
{
    public class MyVirtualFile : VirtualFile
    {
        private string myPath;

        public MyVirtualFile(string virtualPath)
            : base(virtualPath)
        {
            myPath = virtualPath;
        }

        public override Stream Open()
        {
            Stream stream = new MemoryStream();
            StreamWriter sw = new StreamWriter(stream);
            StreamReader sr = null;

            try
            {
                if (myPath.EndsWith("myvirtualfile.aspx"))
                {
                    sr = new StreamReader(
                        HttpContext.Current.Server.MapPath("~/App_Data/htm.txt"), Encoding.UTF8);
                }

                sw.Write(sr.ReadToEnd());
                sw.Flush();
                stream.Seek(0, SeekOrigin.Begin);

                return stream;
            }
            finally
            {
                if (sr != null)
                {
                    sr.Close();
                    sr.Dispose();
                }
            }
        }
    }
}

3.註冊虛擬路徑提供者。
註冊方法有三種。
(1)AppInitialize這個特殊方法中註冊:public static void AppInitialize()
{
    HostingEnvironment.
        RegisterVirtualPathProvider(new MyVirtualPathProvider());
}

(2)在Global.asax的Application_Start方法中註冊。protected void Application_Start(object sender, EventArgs e)
{
    HostingEnvironment.
        RegisterVirtualPathProvider(new MyVirtualPathProvider());
}

(3)繼承IHttpModule,在HttpApplication.BeginRequest的事件處理常式中註冊。public class MyIHttpModule : IHttpModule
{
    public void Dispose()
    {
    }

    public void Init(HttpApplication context)
    {
        context.BeginRequest += new EventHandler(context_BeginRequest);
    }

    void context_BeginRequest(object sender, EventArgs e)
    {
        HostingEnvironment.
            RegisterVirtualPathProvider(new MyVirtualPathProvider());
    }
}

web.config中配置:<httpModules>
    <add name="MyModule"
       type="Xianfen.Net.MyVirtualPathProvider_File.MyIHttpModule, Xianfen.Net.MyVirtualPathProvider_File"/>
</httpModules>

運行可以看到能夠找到虛擬檔案。

參考資料:http://msdn.microsoft.com/zh-cn/library/system.web.hosting.virtualpathprovider.aspx

樣本下載:點擊下載

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.