.ashx文件的作用〔原〕

ashx是用于写web handler的,一般用来处理生成动态图片、 生成动态文本等不需要回传处理的任务。

示例1:比如要做个Ajax的无刷新加法,我使用JQuery做Ajax请求,如下:

JScript code
<script type="text/javascript"> function onAjaxRequest() { $.get("AjaxServer.ashx?a=1&b=2", function callBack(data) { alert(data); }); } </script> <input type="button" value="Click me" onclick="onAjaxRequest();"/>

 

C# code
<%@ WebHandler Language="C#" Class="AjaxServer" %> using System; using System.Web; public class AjaxServer: IHttpHandler { public void ProcessRequest (HttpContext context) { context.Response.ContentType = "text/plain"; int a = int.Prase(context.Reqeust.QueryString["a"]); int b = int.Prase(context.Reqeust.QueryString["b"]); context.Response.Write((a+b).ToString()); } public bool IsReusable { get { return false; } } }



这样就可以,上面便是ashx的一种简单使用,实际开发中他的作用非常大,他直接实现IHttpHandler接口,实际上aspx也是实现了这个接口的。

 

示例2:

 

Code

 

 

给个完整的示例:

 

Code

 

 

注意:在ashx文件中,Response,Request,Server.MapPath等要加上context.

posted @ 2009-06-12 00:47  .NET钉子户  阅读(720)  评论(1)    收藏  举报