C# 一般處理程序 ashx文件

aspx:Web窗體設計頁面。Web窗體頁由兩部分組成:視覺元素(html、服務器控件和靜態文本)和該頁的編程邏輯(VS中的設計視圖和代碼視圖可分別看到它們對應得文件)。VS將這兩個組成部分分別存儲在一個單獨的文件中。視覺元素在.aspx 文件中創建

ashx:.ashx文件是主要用來寫web handler的。使用.ashx 可以讓你專注於編程而不用管相關的web技術。我們熟知的.aspx是要做html控件樹解析的,.aspx包含的所有html實際上是一個類,所有的html都是類裏面的成員,這個過程在.ashx是不需要的。ashx必須包含IsReusable屬性(這個屬性代表是否可複用,通常爲true),而如果要在ashx文件用使用Session必須實現IRequiresSessionState接口.

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

using System;
using System.Web;
using System.Data;
using BLL;
using Comm.Json;

public class StockHandler : IHttpHandler
{
    DataSet ds = new DataSet();
    Mes_ProductBLL mes_ProductBLL = new Mes_ProductBLL();
    Mes_MaterialBLL mes_MaterialBLL = new Mes_MaterialBLL();
    JSONhelper json = new JSONhelper();
    public void ProcessRequest(HttpContext context)
    {
        string output = "";
        string action = context.Request["action"].ToString(); 
        switch (action)
        {
            case "GetProductJson":
                DataTable pdt = getProductData(context);
                string str1 = json.DataTableToJsonWithStringBuilder(pdt);
                output = "{\"total\":" + pdt.Rows.Count + ",\"rows\":" + str1 + "}";
                break;
            case "GetMaterialJson":
                DataTable mdt = getMaterialData(context);
                string str2 = json.DataTableToJsonWithStringBuilder(mdt);
                output = "{\"total\":" + mdt.Rows.Count + ",\"rows\":" + str2 + "}";
                break;

            default:
                break;
        }

        context.Response.ContentType = "text/plain";
        context.Response.Write(output);
    }
    /// <summary>
    /// 獲取產品數據的放法
    /// </summary>
    /// <param name="context"></param>
    /// <returns></returns>
    public DataTable getProductData(HttpContext context)
    {
        DataSet ds = new DataSet();

        if (context.Request["SerchVale"] != null && !string.IsNullOrEmpty(context.Request["SerchVale"].ToString()))
        {
            ds = mes_ProductBLL.GetList(" product_name like '%" + context.Request["SerchVale"].ToString() + "%'");
        }
        else
        {
            ds = mes_ProductBLL.GetList("");
        }
        return ds.Tables[0];
    }
    /// <summary>
    /// 獲取原材料數據的方法
    /// </summary>
    /// <param name="context"></param>
    /// <returns></returns>
    public DataTable getMaterialData(HttpContext context)
    {
        DataSet ds = new DataSet();

        if (context.Request["SerchVale"] != null && !string.IsNullOrEmpty(context.Request["SerchVale"].ToString()))
        {
            ds = mes_MaterialBLL.GetList(" material_name like '%" + context.Request["SerchVale"].ToString() + "%'");
        }
        else
        {
            ds = mes_MaterialBLL.GetList("");
        }
        return ds.Tables[0];
    }
    public bool IsReusable
    {
        get
        {
            return false;
        }
    }

}

ashx特別適合於生成動態圖片,生成動態文本(純文本,json,xml,javascript等即可)等。 
ashx文件有個缺點:它處理控件的回發事件非常麻煩。處理數據的回發,通常都需要一些.aspx頁的功能,只有自己手動處理這些功能(還不如直接建一個aspx文件來處理)。所以,一般使用.ashx輸出一些不需要回發處理的項目即可。 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章