Asp.Net原理 實踐:

來源:互聯網
上載者:User

編程實現中使用Ihttphandler介面:

以設定檔方式指定對特殊http請求處理的類:

<httpHandlers>
<remove verb="*" path="*.asmx"/>
<add verb="POST" path="zx.zx" validate="false" type="HttpHandlerTest"/>
</httpHandlers>

verb:請求的方式,*代表所有的方式。

type屬性由“,”分隔成兩部分,第一部分是實現了介面的類名,第二部分是位於Bin目錄下的編譯過的程式集名稱。

path指的是請求的檔案名稱,可以使用萬用字元擴大範圍,也可以明確指定這個handler僅用於處理某個特定的檔案。

 

IHttpHandler介面:

public interface IHttpHandler

{ ////處理方法
    void ProcessRequest(HttpContext context);
   ////IsReusable屬性,MSDN上是這樣解釋的:擷取一個值,該值指示其他請求是否可以使用 IHttpHandler 執行個體。也就是說後繼的Http請求是不是可以繼續使用實現了該介面的類的執行個體,一般來說,我把它設定成true

    bool IsReusable { get; }
}

 

一般情況下會使用介面:IRequiresSessionState介面,指定當前HttpHandler需要對SessionState值的讀寫訪問權。這是一個標記介面,沒有任何方法)。

HttpHandler的使用:

1.圖片防盜

2.RSS使用

3.自定Ajax實現(下一張中實現)

 

IHttpHandlerFactory工廠介面,減少設定檔的冗長度,但是在iis中配置仍舊不能變少。

class HandlerFactory:IHttpHandlerFactory{
    public IHttpHandler GetHandler(HttpContext context, string requestType, string url, string pathTranslated){
       string path = context.Request.PhysicalPath;
       if (Path.GetExtension(path) == ".rss"){
           return new RSSHandler();
       }

       if (Path.GetExtension(path) == ".atom"){
           return new ATOMHandler();
       }
       return null;
    }

    public void ReleaseHandler(IHttpHandler handler){
    }
}

<httpHandlers>
<add path="*.rss,*.atom" type=" RssFeadsLib.HandlerFactory" verb="GET" />
</httpHandlers>

 

IHttpMoudle介面:

參考http://www.cnblogs.com/JimmyZhang/archive/2007/11/25/971878.

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.