天天看点

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文件。