using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.Security;using System.Web.UI;using System.Web.UI.WebControls;using System.Web.UI.WebControls.WebParts;using System.Web.UI.HtmlControls;using System.Web.SessionState;namespace Test{ public class HandlerTest : IHttpHandler, IRequiresSessionState { public void ProcessRequest(HttpContext context) { context.Response.ClearContent(); context.Response.ContentType = "text/plain"; context.Response.Cache.SetCacheability(HttpCacheability.NoCache); //无缓存 string action = context.Request.Params["action"]; //外部要求 if (action == "modifyPwd") //用户改密码 { string oldPwd = context.Request.Params["pwd"]; //在ashx文件用利用Session必须实现IRequiresSessionState接口 //Session["LogedUser"]是登任命户的会话,用户名和密码都是test if (oldPwd.ToUpper() != ((context.Session["LogedUser"]) as Customer).Password.ToUpper()) //用户输入的旧密码和当前登任命户的不相同 { context.Response.Write("旧密码输入缺点!"); } else { context.Response.Write("旧密码输入精确!"); } } context.Response.End(); } public bool IsReusable { get { return true; } } }}
客户真个调用(js和页面部分):
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="ASHXTest.aspx.cs" Inherits="ASHXTest" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head runat="server"> <title>mytest</title> <script type="text/javascript"> function $(s) { if (document.getElementById) { return eval('document.getElementById("' + s + '")'); } else { return eval('document.all.' + s); } } function createXMLHTTP() { var xmlHttp = false; var arrSignatures = ["MSXML2.XMLHTTP.5.0", "MSXML2.XMLHTTP.4.0", "MSXML2.XMLHTTP.3.0", "MSXML2.XMLHTTP", "Microsoft.XMLHTTP"]; for (var i = 0; i < arrSignatures.length; i++) { try { xmlHttp = new ActiveXObject(arrSignatures[i]); return xmlHttp; } catch (oError) { xmlHttp = false; //ignore } } // throw new Error("MSXML is not installed on your system."); if (!xmlHttp && typeof XMLHttpRequest != 'undefined') { xmlHttp = new XMLHttpRequest(); } return xmlHttp; } var xmlReq = createXMLHTTP(); // 发送ajax处理要求(这里大略验证旧密码的有效性) function validateOldPwd(oTxt) { var url = "/HandlerTest.ashx?action=modifyPwd&pwd=" + escape(oTxt.value); //.ashx文件 xmlReq.open("get", url, true); xmlReq.setRequestHeader("If-Modified-Since", "0"); xmlReq.onreadystatechange = callBack; xmlReq.send(url); // 发送文本 } function callBack() { if (xmlReq.readyState == 4) { if (xmlReq.status == 200) { alert(xmlReq.responseText); // 吸收文本 } else if (xmlReq.status == 404) { alert("Requested URL is not found."); } else if (xmlReq.status == 403) { alert("Access denied."); } else alert("status is " + xmlReq.status); } } </script></head><body> <form id="form1" runat="server"> <div> <input id="txtOldPwd" type="text" onblur="validateOldPwd(this)" /> </div> </form></body></html>
剖析:a、以前我们常日都是通过一个大略的aspx文件实现的功能,实在通过ashx也可以。笔者曾经写过的一篇ajax:数据传输办法简介 ,通过比拟,我们创造aspx要将前后台显示和处理逻辑分开,以是就弄成了两个文件,实在,在终极编译的时候,aspx和cs还是会编译到同一个类中去.这中间就要设计html的一些逻辑处理;而ashx不同,它只是大略的对web http要求的直接返回你想要返回的结果.比aspx少处理了html的过程(但是ashx也可以处理html的一些逻辑,只是常日都不这么用)。理论上ashx比aspx要快。b、还是在相同的旧文里,我们知道数据传输的几种办法,实在ashx都可以实现(修正ashx文件里context.Response.ContentType 即可),这里不再赘述了。(2)、ashx特殊适宜于天生动态图片,天生动态文本(纯文本,json,xml,javascript等即可)等。 (3)、.ashx文件有个缺陷:它处理控件的回发事宜非常麻烦。处理数据的回发,常日都须要一些.aspx页的功能,只有自己手动处理这些功能(还不如直接建一个aspx文件来处理)。以是,一样平常利用.ashx输出一些不须要回发处理的项目即可。 4、总结aspx-->P(Page)ascx-->C(Control)ashx-->H(HttpHandler)
当浏览器访问web做事器,我们终极吸收到的还是html文本。浏览器通过渲染引擎阐明这些标签,在屏幕上展现出可见的效果。而asp.net不过便是我们运用的一种平台技能来"变相"阐明html的,说白了它便是为了提高生产率,它的技能术语再多,实质上还是html范畴内的东西(如果你不通过那些动态页面技能完备利用html和浏览器(当然包括js)技能来实现动态页面效果,那么你会创造效果有了代码量也相称可不雅观).以是说web开拓的底层便是一堆的html标签,无论是asp.net还是jsp都是对html某种办法的包装,是html的产物。
