天天看點

ashx是什麼檔案,什麼時候使用ashx

.ashx應用:

處理生成動态圖檔、 生成動态文本等不需要回傳處理的任務

處理ajax請求

可以用ashx檔案建立web 服務。類似web servers 。比如傳輸json格式的資料

輕量的資訊互動都可以用這個,沒有aspx那麼複雜的生命周期

ashx是什麼檔案,什麼時候使用ashx

<%@ WebHandler Language="C#" Class="ImageHandler" %>

ashx是什麼檔案,什麼時候使用ashx
ashx是什麼檔案,什麼時候使用ashx

using System;

ashx是什麼檔案,什麼時候使用ashx

using System.Web;

ashx是什麼檔案,什麼時候使用ashx

/// <summary>

ashx是什麼檔案,什麼時候使用ashx

/// 這就一個沒有任何實作的一般處理程式。

ashx是什麼檔案,什麼時候使用ashx

/// </summary>

ashx是什麼檔案,什麼時候使用ashx

public class ImageHandler : IHttpHandler {

ashx是什麼檔案,什麼時候使用ashx
ashx是什麼檔案,什麼時候使用ashx

    public void ProcessRequest (HttpContext context)

ashx是什麼檔案,什麼時候使用ashx

    {

ashx是什麼檔案,什麼時候使用ashx

        //擷取虛拟目錄的實體路徑。 

ashx是什麼檔案,什麼時候使用ashx

        string path = context.Server.MapPath("");

ashx是什麼檔案,什麼時候使用ashx

        //擷取圖檔檔案的二進制資料。

ashx是什麼檔案,什麼時候使用ashx

        byte[] datas = System.IO.File.ReadAllBytes(path + "\\U1513.jpg");

ashx是什麼檔案,什麼時候使用ashx

       //将二進制資料寫入到輸出流中。

ashx是什麼檔案,什麼時候使用ashx

        context.Response.OutputStream.Write(datas, 0, datas.Length);

ashx是什麼檔案,什麼時候使用ashx

    }

ashx是什麼檔案,什麼時候使用ashx
ashx是什麼檔案,什麼時候使用ashx

    public bool IsReusable {

ashx是什麼檔案,什麼時候使用ashx

        get {

ashx是什麼檔案,什麼時候使用ashx

            return false;

ashx是什麼檔案,什麼時候使用ashx

        }

ashx是什麼檔案,什麼時候使用ashx
ashx是什麼檔案,什麼時候使用ashx
ashx是什麼檔案,什麼時候使用ashx

}

default.aspx檔案

注意上面的代碼:<asp:Image ID="Image1" runat="server"  ImageUrl="~/ImageHandler.ashx"/></div> 中ImageUrl指向的是ImageHandler.ashx檔案。